Java format BigDecimal numbers with comma and 2 decimal place
27,028
Solution 1
This should be enough to get the results.
String.format("%,.2f", amount.setScale(2, RoundingMode.DOWN));
Solution 2
You should use DecimalFormat
:
val df: DecimalFormat = DecimalFormat("#,##0.000")
df.decimalFormatSymbols = DecimalFormatSymbols(Locale.getDefault())
df.format(BigDecimal(123456,78)); //will output 123.456,78 - depending on Locale
Author by
AmbGup
Updated on July 05, 2022Comments
-
AmbGup almost 2 years
I want to format BigDecimal Numbers with comma and 2 decimal points. e.g.
Amount is: 5.0001 and formatted to: 5.00 Amount is: 999999999.999999 and formatted to: 999,999,999.99 Amount is: 1000.4999 and formatted to: 1,000.49 Amount is: 9999.089 and formatted to: 9,999.08 Amount is: 0.19999 and formatted to: 0.19 Amount is: 123456.99999999 and formatted to: 123,456.99
-
Pshemo over 9 yearsOutput depends on locale, so maybe use
format(Locale.ENGLISH,"%,.2f", amount.setScale(2, RoundingMode.DOWN))
. If you change locale toFRANCE
you will see little different formatting.