0

I would like to know if it's possible for us to add commas on BigDecimal while retaining number of decimals and trailing zeros?

1111.90 --> 1,111.90
1234.010 --> 1,234.010
938938.1223 --> 938,938.1223

I tried using DecimalFormat and it seems to be removing trailing zeros.

DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();

symbols.setGroupingSeparator(',');
formatter.setDecimalFormatSymbols(symbols);
System.out.println(formatter.format(bd));
stackyyflow
  • 683
  • 3
  • 9
  • 27
  • Does this answer your question? [How to set thousands separator in Java?](https://stackoverflow.com/questions/5323502/how-to-set-thousands-separator-in-java) – rajah9 Jan 25 '22 at 14:13
  • 1
    You could try setting `formatter.setMinimumFractionDigits(bd.scale());` and see if that gives you the result you want. (Should work for most values, but I haven't tested it and make no guarantees) – OH GOD SPIDERS Jan 25 '22 at 14:17
  • @OHGODSPIDERS thank you, seems like it works! :) – stackyyflow Jan 25 '22 at 14:27

0 Answers0