How do I make 0 display as 0.00 using decimal format?

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

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

      

For other numbers this is fine, but 0 is returned as ".00" I want it to be "0.00". What do I need to change?

+3


source to share


2 answers


Why not

return String.format("%.2f", amount);

      

Would that format it correctly? (if the sum is 123123.14233 then it will return 123123.14)



or

return String.format("%,.2f", amount); 

      

for commas inside the number. (if the sum is 123123.14233 then it will return 123,123.14)

+4


source


#

means an optional digit, so if you use 0

it will work:

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

      



BTW: I think you need 3 ###

not 4.

+6


source







All Articles