How to format decimals in a currency format?
You can use the NumberFormat
class to format decimals in a currency format. Here's an example of how you can use it:
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
double amount = 123.45;
String formattedAmount = currencyFormat.format(amount);
System.out.println(formattedAmount); // prints "$123.45"
Note that the currency symbol and the formatting of the number will depend on the default locale of the system. You can specify a specific locale if you want to use a different currency symbol or formatting. For example:
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
double amount = 123.45;
String formattedAmount = currencyFormat.format(amount);
System.out.println(formattedAmount); // prints "123,45 €"