How to print formatted BigDecimal values?
bigdecimal, formatting, java, number-formatting
Solution
public static String currencyFormat(BigDecimal n) {
return NumberFormat.getCurrencyInstance().format(n);
}
It will use your JVM’s current default `Locale` to choose your currency symbol. Or you can specify a `Locale`.
NumberFormat.getInstance(Locale.US)
For more info, see `NumberFormat` class.
Problem
I have a `BigDecimal` field `amount` which represents money, and I need to print its value in the browser in a format like `$123.00`, `$15.50`, `$0.33`. How can I do that? (The only simple solution which I see myself is getting `floatValue` from `BigDecimal` and then using `NumberFormat` to make two-digit precision for the fraction part).