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

前端 未结 14 879
一整个雨季
一整个雨季 2020-11-21 11:52

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

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

It shows this

14条回答
  •  故里飘歌
    2020-11-21 12:07

    You could use printf() with %f:

    double dexp = 12345678;
    System.out.printf("dexp: %f\n", dexp);
    

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

    System.out.printf("dexp: %.0f\n", 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.

提交回复
热议问题