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

前端 未结 14 831
一整个雨季
一整个雨季 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 11:56

    I needed to convert some double to currency values and found that most of the solutions were OK, but not for me.

    The DecimalFormat was eventually the way for me, so here is what I've done:

       public String foo(double value) //Got here 6.743240136E7 or something..
        {
            DecimalFormat formatter;
    
            if(value - (int)value > 0.0)
                formatter = new DecimalFormat("0.00"); // Here you can also deal with rounding if you wish..
            else
                formatter = new DecimalFormat("0");
    
            return formatter.format(value);
        }
    

    As you can see, if the number is natural I get - say - 20000000 instead of 2E7 (etc.) - without any decimal point.

    And if it's decimal, I get only two decimal digits.

提交回复
热议问题