JDK-5078240 : Double.toString(double) adds a trailing zero in certain cases
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-07-26
  • Updated: 2004-07-27
  • Resolved: 2004-07-27
Related Reports
Duplicate :  
Relates :  
Description
Name: js151677			Date: 07/26/2004


FULL PRODUCT VERSION :
java version "1.4.2_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
When a double value that has a single third digit of precision is converted to a String using Double.toString(double), an extra zero is appended to the String at the fourth digit of precision.  For example, the double value 0.004 will be converted to "0.0040".  This does not appear to happen when there is a first and/or second digit of precision present or an integer portion, nor does this happen when there is only a single digit of precision at any other position.  The following table outlines the abnormality:

        double                Double.toString(double)                        result
        0.004                 0.0040                                fail
        0.044                 0.044                                 okay
        0.404                 0.404                                 okay
        4.004                 4.004                                 okay
        0.4                   0.4                                   okay
        0.04                  0.04                                  okay
        0.0004                4.0E-4                                okay


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the test program included below.


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Double.toString(double) should never include unnecessary trailing zeros.  In the example provided, the double 0.004 should produce the String "0.004".
ACTUAL -
Double.toString(double) adds a trailing zero at the fourth level of precision for any double that has only one single non-zero digit which is at the third level of precision.   In the example provided, the double 0.004 produces the String "0.0040".


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class DoubleTrouble {
public static void main (String[] args) {
	System.out.print("0.004  -> ");
	System.out.println(Double.toString(0.004d));
	System.out.print("0.044  -> ");
	System.out.println(Double.toString(0.044d));
	System.out.print("0.404  -> ");
	System.out.println(Double.toString(0.404d));
	System.out.print("4.004  -> ");
	System.out.println(Double.toString(4.004d));
	System.out.print("0.4    -> ");
	System.out.println(Double.toString(0.4d));
	System.out.print("0.04   -> ");
	System.out.println(Double.toString(0.04d));
	System.out.print("0.0004 -> ");
	System.out.println(Double.toString(0.0004d));
}
}

---------- END SOURCE ----------
(Incident Review ID: 286698) 
======================================================================