How to print formatted BigDecimal values?
JavaFormattingBigdecimalNumber FormattingJava Problem Overview
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).
Java Solutions
Solution 1 - Java
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.
Solution 2 - Java
To set thousand separator, say 123,456.78
you have to use DecimalFormat:
DecimalFormat df = new DecimalFormat("#,###.00");
System.out.println(df.format(new BigDecimal(123456.75)));
System.out.println(df.format(new BigDecimal(123456.00)));
System.out.println(df.format(new BigDecimal(123456123456.78)));
Here is the result:
123,456.75
123,456.00
123,456,123,456.78
Although I set #,###.00
mask, it successfully formats the longer values too.
Note that the comma(,) separator in result depends on your locale. It may be just space( ) for Russian locale.
Solution 3 - Java
Another way which could make sense for the given situation is
BigDecimal newBD = oldBD.setScale(2);
I just say this because in some cases when it comes to money going beyond 2 decimal places does not make sense. Taking this a step further, this could lead to
String displayString = oldBD.setScale(2).toPlainString();
but I merely wanted to highlight the setScale method (which can also take a second rounding mode argument to control how that last decimal place is handled. In some situations, Java forces you to specify this rounding method).
Solution 4 - Java
BigDecimal pi = new BigDecimal(3.14);
BigDecimal pi4 = new BigDecimal(12.56);
System.out.printf("%.2f",pi);
// prints 3.14
System.out.printf("%.0f",pi4);
// prints 13
Solution 5 - Java
Similar to answer by @Jeff_Alieffson, but not relying on default Locale
:
Use DecimalFormatSymbols
for explicit locale:
DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(new Locale("ru", "RU"));
Or explicit separator symbols:
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
decimalFormatSymbols.setDecimalSeparator('.');
decimalFormatSymbols.setGroupingSeparator(' ');
Then:
new DecimalFormat("#,##0.00", decimalFormatSymbols).format(new BigDecimal("12345"));
Result:
12 345.00
Solution 6 - Java
BigDecimal(19.0001).setScale(2, BigDecimal.RoundingMode.DOWN)
Solution 7 - Java
I know this question is very old, but I was making similar thing in my kotlin app recently. So here is an example if anyone needs it:
val dfs = DecimalFormatSymbols.getInstance(Locale.getDefault())
val bigD = BigDecimal("1e+30")
val formattedBigD = DecimalFormat("#,##0.#",dfs).format(bigD)
Result displaying $formattedBigD:
1,000,000,000,000,000,000,000,000,000,000