Hi,

I believe the attached patch fixes the trailing zero problem mentioned
in several bugs (in addition to $SUBJECT), including 4511638, 6575880,
and 4191279.

The problem arises because sun.misc.FloatingDecimal.getChars and sun.misc.FloatingDecimal.dtoa disagree about when numbers are represented in E-form and when they are not. As a result, dtoa stuffs one too many numbers (a '0' in this case) into digits[], and getChars blindly returns them to FloatingDecimal.toJavaFormatString.

I have also attached a jtreg test for this, based on various testcases given in several bug requests.

Keith
diff -r ea4661174f85 j2se/src/share/classes/sun/misc/FloatingDecimal.java
--- a/j2se/src/share/classes/sun/misc/FloatingDecimal.java	Wed Sep 12 18:10:19 2007 -0700
+++ b/j2se/src/share/classes/sun/misc/FloatingDecimal.java	Tue Sep 25 12:34:46 2007 -0700
@@ -730,7 +730,7 @@ public class FloatingDecimal{
 		 * Thus we will need more than one digit if we're using
 		 * E-form
 		 */
-		if ( decExp <= -3 || decExp >= 8 ){
+		if ( decExp < -3 || decExp >= 8 ){
 		    high = low = false;
 		}
 		while( ! low && ! high ){
@@ -783,7 +783,7 @@ public class FloatingDecimal{
 		 * Thus we will need more than one digit if we're using
 		 * E-form
 		 */
-		if ( decExp <= -3 || decExp >= 8 ){
+		if ( decExp < -3 || decExp >= 8 ){
 		    high = low = false;
 		}
 		while( ! low && ! high ){
@@ -847,7 +847,7 @@ public class FloatingDecimal{
 	     * Thus we will need more than one digit if we're using
 	     * E-form
 	     */
-	    if ( decExp <= -3 || decExp >= 8 ){
+	    if ( decExp < -3 || decExp >= 8 ){
 		high = low = false;
 	    }
 	    while( ! low && ! high ){
/**
 * @test
 * @bug 4428022 4511638 6575880 4191279
 * @summary Float/Double.toString should not output trailing zeros
 */
public class FloatDoubleTrailingZeros {
  public static void main(String[] args) throws Exception {
    String s;

    // Test Float.toString
    s = Float.toString(0.001f);
    if (!s.equals("0.001")) {
      System.out.println ("Float.toString(0.001f) = " + s);
      throw new Exception("Float.toString(\"0.001\") != \"0.001\"");
    }

    s = Float.toString(1.001f);
    if (!s.equals("1.001")) {
      System.out.println ("Float.toString(1.001f) = " + s);
      throw new Exception("Float.toString(\"1.001\") != \"1.001\"");
    }

    s = Float.toString(123.123f);
    if (!s.equals("123.123")) {
      System.out.println("Float.toString(123.123) = " + s);
      throw new Exception("Float.toString(\"123.123\" != \"123.123\"");
    }

    s = Float.toString(0.1f);
    if (!s.equals("0.1")) {
      System.out.println ("Float.toString(0.1f) = " + s);
      throw new Exception("Float.toString(\"0.1\") != \"0.1\"");
    }

    s = Float.toString(0.01f);
    if (!s.equals("0.01")) {
      System.out.println ("Float.toString(0.01f) = " + s);
      throw new Exception("Float.toString(\"0.01\") != \"0.01\"");
    }

    s = Float.toString(0.0001f);
    if (!s.equals("1.0E-4")) {
      System.out.println ("Float.toString(0.0001f) = " + s);
      throw new Exception("Float.toString(\"0.0001\") != \"1.0E-4\"");
    }

    // Test Double.toString
    s = Double.toString(0.001);
    if (!s.equals("0.001")) {
      System.out.println ("Double.toString(0.001) = " + s);
      throw new Exception("Double.toString(\"0.001\") != \"0.001\"");
    }

    s = Double.toString(1.001);
    if (!s.equals("1.001")) {
      System.out.println ("Double.toString(1.001) = " + s);
      throw new Exception("Double.toString(\"1.001\") != \"1.001\"");
    }

    s = Double.toString(123.123);
    if (!s.equals("123.123")) {
      System.out.println("Double.toString(123.123) = " + s);
      throw new Exception("Double.toString(\"123.123\" != \"123.123\"");
    }

    s = Double.toString(0.1);
    if (!s.equals("0.1")) {
      System.out.println ("Double.toString(0.1) = " + s);
      throw new Exception("Double.toString(\"0.1\") != \"0.1\"");
    }

    s = Double.toString(0.01);
    if (!s.equals("0.01")) {
      System.out.println ("Double.toString(0.01) = " + s);
      throw new Exception("Double.toString(\"0.01\") != \"0.01\"");
    }

    s = Double.toString(0.0001);
    if (!s.equals("1.0E-4")) {
      System.out.println ("Double.toString(0.0001) = " + s);
      throw new Exception("Double.toString(\"0.0001\") != \"1.0E-4\"");
    }
  }
}

Reply via email to