src/java.base/share/classes/java/util/Formatter.java

Print this page
rev 11172 : 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: TBD

@@ -3730,28 +3730,31 @@
                     return;
                 }
                 long adjusted = -(long) scale + (len - 1);
                 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);
+                            if (intVal.signum() != 0) {
                             trailingZeros(mant, -scale);
+                            }
                             this.scale = 0;
                         }
                     }
                 } else {
                     // x.xxx form