Richard Richard - 1 year ago 51
Java Question

Java Double Format to show 2 decimal points even when zero

I want to print out double values onto the screen with 2 decimal points. So far this line has worked perfectly:

System.out.println(new DecimalFormat("$#,###.##").format(value));

For inputs like:

double value = 82348238482834.23482348;

It prints out:


Which is exactly what I want. However, if I have the input
I want it to print out
. With the above format line it prints out
. I tried changing the above code to:

double value = 0;
System.out.println(new DecimalFormat("$#,###.00").format(value));

But that printed out
. I could just do this:

if(value == 0) {

It works but it's very ugly. Is there any other way to solve this issue?

Answer Source


System.out.println(new DecimalFormat("$#,##0.00").format(value));



Like Jon said, 0 indicates a digit so it includes zero, # indicates a digit but zero shows as absent.

For more info on DecimalFormat.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download