JDK-4156542 : Double.toString returns incorrect result (affects String too)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.2.0
  • Priority: P1
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 1998-07-11
  • Updated: 1998-07-13
  • Resolved: 1998-07-13
Related Reports
Relates :  
Description

Name: mf23781			Date: 07/11/98


The Float and Double classes' toString (and the String and other
classes that uses these) produce results that do not match the
JLS (or the BigDecimal class, which has its own code for 
conversion from Float and Double, for some reason); a trailing .0
supplied for non-zero results.
This applies to both 1.1 and 1.2.  

For example, the testcase:

 import java.math.BigDecimal;
 import java.lang.Double;
 class testd2s {
  public static void main(String s[]){
   double d=100D;
   BigDecimal bd=new BigDecimal(d);
   System.out.println(bd.toString());
   System.out.println(new Double(d).toString());
  }
 }

displays:

 100
 100.0

(BigDecimal is giving the right answer; Double is not.)

The problem would seem to be in the (undocumented?) FloatingDecimal class.
======================================================================

Comments
WORK AROUND Name: mf23781 Date: 07/11/98 Manual fixup of all strings produced in this way by testing for ending in ".0" and removing if the first digit is not '0'. (Slow!) ======================================================================
11-06-2004

PUBLIC COMMENTS User misunderstanding of Java Language Specification.
10-06-2004

EVALUATION Java Language Specification section 20.9.16, on java.lang.Float.toString(float f) ... If m is greater than or equal to 10^-3 but less than 10^7, then it is respresented as the integer part of m, in decimal form with no leading zeroes, followed by '.' (\u002E), followed by one or more decimal digits representing the fractional part of m. In this sentence, "one or more" means one, or more. In other words, there is always a . and at least one fractional digit for numbers in the range 0.001 <= m < 10000000. And sometimes, it is a zero. Java Language Specification section 20.10.15, on java.lang.Double.toString(double d) contains identical wording. richard.tuck@Eng 1998-07-13
13-07-1998