Skip to content
Advertisement

How do I print a double value without scientific notation using Java?

I want to print a double value in Java without exponential form.

double dexp = 12345678;
System.out.println("dexp: "+dexp);

It shows this E notation: 1.2345678E7.

I want it to print it like this: 12345678

What is the best way to prevent this?

Advertisement

Answer

You could use printf() with %f:

double dexp = 12345678;
System.out.printf("dexp: %fn", dexp);

This will print dexp: 12345678.000000. If you don’t want the fractional part, use

System.out.printf("dexp: %.0fn", dexp);

0 in %.0f means 0 places in fractional part i.e no fractional part. If you want to print fractional part with desired number of decimal places then instead of 0 just provide the number like this %.8f. By default fractional part is printed up to 6 decimal places.

This uses the format specifier language explained in the documentation.

The default toString() format used in your original code is spelled out here.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement