--- old/src/share/classes/java/util/Formatter.java 2013-06-24 11:02:07.000000000 -0700 +++ new/src/share/classes/java/util/Formatter.java 2013-06-24 11:02:07.000000000 -0700 @@ -3297,18 +3297,29 @@ else if (precision == 0) prec = 1; - FormattedFloatingDecimal fd + char[] exp; + char[] mant; + int expRounded; + if (value == 0.0) { + exp = null; + mant = new char[] {'0'}; + expRounded = 0; + } else { + FormattedFloatingDecimal fd = FormattedFloatingDecimal.valueOf(value, prec, FormattedFloatingDecimal.Form.GENERAL); + exp = fd.getExponent(); + mant = fd.getMantissa(); + expRounded = fd.getExponentRounded(); + } - char[] exp = fd.getExponent(); if (exp != null) { prec -= 1; } else { - prec = prec - (value == 0 ? 0 : fd.getExponentRounded()) - 1; + prec -= expRounded + 1; } - char[] mant = addZeros(fd.getMantissa(), prec); + mant = addZeros(mant, prec); // If the precision is zero and the '#' flag is set, add the // requested decimal point. if (f.contains(Flags.ALTERNATE) && (prec == 0)) --- old/test/java/util/Formatter/Basic-X.java.template 2013-06-24 11:02:09.000000000 -0700 +++ new/test/java/util/Formatter/Basic-X.java.template 2013-06-24 11:02:09.000000000 -0700 @@ -1177,6 +1177,13 @@ test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0))); + test("%.1g", "-0", -0.0); + test("%3.0g", " -0", -0.0); + test("%.1g", "0", 0.0); + test("%3.0g", " 0", 0.0); + test("%.1g", "0", +0.0); + test("%3.0g", " 0", +0.0); + test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.0000099); --- old/test/java/util/Formatter/Basic.java 2013-06-24 11:02:11.000000000 -0700 +++ new/test/java/util/Formatter/Basic.java 2013-06-24 11:02:10.000000000 -0700 @@ -25,7 +25,7 @@ * @summary Unit test for formatter * @bug 4906370 4962433 4973103 4989961 5005818 5031150 4970931 4989491 5002937 * 5005104 5007745 5061412 5055180 5066788 5088703 6317248 6318369 6320122 - * 6344623 6369500 6534606 6282094 6286592 6476425 5063507 + * 6344623 6369500 6534606 6282094 6286592 6476425 5063507 6469160 * * @run shell/timeout=240 Basic.sh */ --- old/test/java/util/Formatter/BasicBigDecimal.java 2013-06-24 11:02:12.000000000 -0700 +++ new/test/java/util/Formatter/BasicBigDecimal.java 2013-06-24 11:02:12.000000000 -0700 @@ -1177,6 +1177,13 @@ test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0))); + test("%.1g", "-0", -0.0); + test("%3.0g", " -0", -0.0); + test("%.1g", "0", 0.0); + test("%3.0g", " 0", 0.0); + test("%.1g", "0", +0.0); + test("%3.0g", " 0", +0.0); + test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.0000099); --- old/test/java/util/Formatter/BasicDouble.java 2013-06-24 11:02:14.000000000 -0700 +++ new/test/java/util/Formatter/BasicDouble.java 2013-06-24 11:02:14.000000000 -0700 @@ -1123,6 +1123,15 @@ + + + + + + + + + //--------------------------------------------------------------------- // %f - float, double, Double, BigDecimal //--------------------------------------------------------------------- @@ -1168,6 +1177,13 @@ test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0))); + test("%.1g", "-0", -0.0); + test("%3.0g", " -0", -0.0); + test("%.1g", "0", 0.0); + test("%3.0g", " 0", 0.0); + test("%.1g", "0", +0.0); + test("%3.0g", " 0", +0.0); + test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.0000099); --- old/test/java/util/Formatter/BasicDoubleObject.java 2013-06-24 11:02:16.000000000 -0700 +++ new/test/java/util/Formatter/BasicDoubleObject.java 2013-06-24 11:02:15.000000000 -0700 @@ -1123,6 +1123,15 @@ + + + + + + + + + //--------------------------------------------------------------------- // %f - float, double, Double, BigDecimal //--------------------------------------------------------------------- @@ -1168,6 +1177,13 @@ test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0))); + test("%.1g", "-0", -0.0); + test("%3.0g", " -0", -0.0); + test("%.1g", "0", 0.0); + test("%3.0g", " 0", 0.0); + test("%.1g", "0", +0.0); + test("%3.0g", " 0", +0.0); + test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.0000099); --- old/test/java/util/Formatter/BasicFloat.java 2013-06-24 11:02:17.000000000 -0700 +++ new/test/java/util/Formatter/BasicFloat.java 2013-06-24 11:02:17.000000000 -0700 @@ -1106,6 +1106,15 @@ + + + + + + + + + //--------------------------------------------------------------------- // %f - float //--------------------------------------------------------------------- @@ -1168,6 +1177,13 @@ test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0))); + test("%.1g", "-0", -0.0); + test("%3.0g", " -0", -0.0); + test("%.1g", "0", 0.0); + test("%3.0g", " 0", 0.0); + test("%.1g", "0", +0.0); + test("%3.0g", " 0", +0.0); + test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.0000099); --- old/test/java/util/Formatter/BasicFloatObject.java 2013-06-24 11:02:19.000000000 -0700 +++ new/test/java/util/Formatter/BasicFloatObject.java 2013-06-24 11:02:18.000000000 -0700 @@ -1139,6 +1139,15 @@ + + + + + + + + + //--------------------------------------------------------------------- // %g // @@ -1168,6 +1177,13 @@ test("%.3G", "1.00E-05", recip(create(100000.0))); test("%.3G", "-1.00E-05", recip(create(-100000.0))); + test("%.1g", "-0", -0.0); + test("%3.0g", " -0", -0.0); + test("%.1g", "0", 0.0); + test("%3.0g", " 0", 0.0); + test("%.1g", "0", +0.0); + test("%3.0g", " 0", +0.0); + test("%3.0g", "1e-06", 0.000001); test("%3.0g", "1e-05", 0.00001); test("%3.0g", "1e-05", 0.0000099);