8059175: Zero BigDecimal with negative scale prints leading zeroes in String.format
authorbpb
Wed, 07 Jan 2015 14:15:00 -0800
changeset 28302 5de69b0d3a84
parent 28301 6a8523efd309
child 28303 641c5c22c491
8059175: Zero BigDecimal with negative scale prints leading zeroes in String.format Summary: Correct erroneous appending of zeroes and clean up code logic. Reviewed-by: darcy, psandoz
jdk/src/java.base/share/classes/java/util/Formatter.java
jdk/test/java/util/Formatter/Basic-X.java.template
jdk/test/java/util/Formatter/Basic.java
jdk/test/java/util/Formatter/BasicBigDecimal.java
--- a/jdk/src/java.base/share/classes/java/util/Formatter.java	Wed Jan 07 09:32:41 2015 -0800
+++ b/jdk/src/java.base/share/classes/java/util/Formatter.java	Wed Jan 07 14:15:00 2015 -0800
@@ -3727,29 +3727,29 @@
                             exp = new StringBuilder("+00");
                         }
                     }
-                    return;
-                }
-                long adjusted = -(long) scale + (len - 1);
-                if (form == BigDecimalLayoutForm.DECIMAL_FLOAT) {
+                } else if (form == BigDecimalLayoutForm.DECIMAL_FLOAT) {
                     // count of padding zeros
-                    int pad = scale - len;
-                    if (pad >= 0) {
+
+                    if (scale >= len) {
                         // 0.xxx form
                         mant.append("0.");
                         dot = true;
-                        trailingZeros(mant, pad);
+                        trailingZeros(mant, scale - len);
                         mant.append(coeff);
                     } else {
-                        if (-pad < len) {
+                        if (scale > 0) {
                             // xx.xx form
-                            mant.append(coeff, 0, -pad);
+                            int pad = len - scale;
+                            mant.append(coeff, 0, pad);
                             mant.append('.');
                             dot = true;
-                            mant.append(coeff, -pad, -pad + scale);
-                        } else {
+                            mant.append(coeff, pad, len);
+                        } else { // scale < 0
                             // xx form
                             mant.append(coeff, 0, len);
-                            trailingZeros(mant, -scale);
+                            if (intVal.signum() != 0) {
+                                trailingZeros(mant, -scale);
+                            }
                             this.scale = 0;
                         }
                     }
@@ -3762,6 +3762,7 @@
                         mant.append(coeff, 1, len);
                     }
                     exp = new StringBuilder();
+                    long adjusted = -(long) scale + (len - 1);
                     if (adjusted != 0) {
                         long abs = Math.abs(adjusted);
                         // require sign
--- a/jdk/test/java/util/Formatter/Basic-X.java.template	Wed Jan 07 09:32:41 2015 -0800
+++ b/jdk/test/java/util/Formatter/Basic-X.java.template	Wed Jan 07 14:15:00 2015 -0800
@@ -1154,6 +1154,10 @@
         test("%.5f", "0.99960", val);
         test("%.6f", "0.999600", val);
 
+        val = new BigDecimal(BigInteger.ZERO, 6);
+        test("%.4f", "0.0000", val);
+        val = new BigDecimal(BigInteger.ZERO, -6);
+        test("%.4f", "0.0000", val);
 #end[BigDecimal]
 
 #if[float]
--- a/jdk/test/java/util/Formatter/Basic.java	Wed Jan 07 09:32:41 2015 -0800
+++ b/jdk/test/java/util/Formatter/Basic.java	Wed Jan 07 14:15:00 2015 -0800
@@ -26,6 +26,7 @@
  * @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 6469160 6476168
+ *      8059175
  *
  * @run shell/timeout=240 Basic.sh
  */
--- a/jdk/test/java/util/Formatter/BasicBigDecimal.java	Wed Jan 07 09:32:41 2015 -0800
+++ b/jdk/test/java/util/Formatter/BasicBigDecimal.java	Wed Jan 07 14:15:00 2015 -0800
@@ -1154,6 +1154,11 @@
         test("%.5f", "0.99960", val);
         test("%.6f", "0.999600", val);
 
+        val = new BigDecimal(BigInteger.ZERO, 6);
+        test("%.4f", "0.0000", val);
+
+        val = new BigDecimal(BigInteger.ZERO, -6);
+        test("%.4f", "0.0000", val);