Java7 Double.toString() returns 0.005 / java6 it is 0.0050

给你一囗甜甜゛ 提交于 2019-11-29 06:24:08

问题


I am upgrading from JDK6 to JDK7. The following code demonstrate shows a minor change in Double.toString()

public class StringDemo
{

    public static void main(String[] args)
    {
        System.out.println(Double.toString(.0005));
        System.out.println(Double.toString(.005)); //different string
        System.out.println(Double.toString(.05));
        System.out.println(Double.toString(.5));
    }
}

JRE6

5.0E-4
0.0050
0.05
0.5

JRE7

I am looking for any documentation related to above change. The compatibility page does not cover it.

5.0E-4
0.005   //changed.
0.05
0.5

The output was saved in many reference files, and compared by string comparison- I need to fix the comparison, but curious to know more details about this change. Authoritative answer on why this change will get bounty.


回答1:


This was a bug in Java 1.3 through 1.6 (resolved in 1.7).

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4511638 The bug report http://bugs.sun.com/view_bug.do?bug_id=4428022 contains more details. Fixed in JDK 7 (b75).

Related Reports- Quoted from the link above.

  • Backport: JDK-2181423 - System.out.println(0.001) outputs 0.0010
  • Duplicate: JDK-5078240 - Double.toString(double) adds a trailing zero in certain cases
  • Duplicate: JDK-6575880 - Float.toString(float) adds trailing zeros
  • Relates: JDK-6935102 - Regtest
    closed/sun/misc/FloatingDecimal/ToString.java now failing.
  • Relates: JDK-4154042 - java.lang.FloatingDecimal could be eliminated

The changes for OpenJDK 7 to fix this issue are available at: http://hg.openjdk.java.net/jdk7/tl/jdk/rev/f85aa3aedf41



来源:https://stackoverflow.com/questions/15812504/java7-double-tostring-returns-0-005-java6-it-is-0-0050

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!