How to make 0 display as 0.00 using decimal format?

Hiten Naresh Vasnani picture Hiten Naresh Vasnani · Nov 3, 2014 · Viewed 24.8k times · Source

I am using the following code to make numbers display with two decimal places and thousands comma separator.

public static String formatNumber(double amount){
    DecimalFormat formatter = new DecimalFormat("#,###.00");
    return formatter.format(amount);
}

For other numbers it is ok but 0 is returned as ".00" I want it to be "0.00" What should I change?

Answer

eckes picture eckes · Nov 3, 2014

The # means optional digit, so if you use 0 instead it will work:

    DecimalFormat formatter = new DecimalFormat("#,##0.00");

BTW: I think you need 3 ### not 4.