How can I convert String to Double without losing precision in Java?

后端 未结 4 1726
北恋
北恋 2020-12-18 07:05

Tried as below

String d=new String(\"12.00\");
Double dble =new Double(d.valueOf(d));
System.out.println(dble);

Output: 12.0

But i

4条回答
  •  离开以前
    2020-12-18 08:07

    If you want to format output, use PrintStream#format(...):

    System.out.format("%.2f%n", dble);
    

    There %.2f - two places after decimal point and %n - newline character.

    UPDATE:

    If you don't want to use PrintStream#format(...), use DecimalFormat#format(...).

提交回复
热议问题