Format a BigDecimal as String with max 2 decimal digits, removing 0 on decimal part

I used DecimalFormat for formatting the BigDecimal instead of formatting the String, seems no problems with it.

The code is something like this:

bd = bd.setScale(2, BigDecimal.ROUND_DOWN);

DecimalFormat df = new DecimalFormat();
            
df.setMaximumFractionDigits(2);
            
df.setMinimumFractionDigits(0);
            
df.setGroupingUsed(false);

String result = df.format(bd);

Leave a Comment