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

前端 未结 14 857
一整个雨季
一整个雨季 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:15

    I had this same problem in my production code when I was using it as a string input to a math.Eval() function which takes a string like "x + 20 / 50"

    I looked at hundreds of articles... In the end I went with this because of the speed. And because the Eval function was going to convert it back into its own number format eventually and math.Eval() didn't support the trailing E-07 that other methods returned, and anything over 5 dp was too much detail for my application anyway.

    This is now used in production code for an application that has 1,000+ users...

    double value = 0.0002111d;
    String s = Double.toString(((int)(value * 100000.0d))/100000.0d); // Round to 5 dp
    
    s display as:  0.00021
    

提交回复
热议问题