6476425: (fmt)java.util.Formatter.print() throws IllegalArgumentException on large BigDecima
authorsherman
Thu, 22 Jan 2009 20:29:54 -0800
changeset 1823 23d42a96635e
parent 1822 dd4ba253de9f
child 1824 7a685390c6ab
6476425: (fmt)java.util.Formatter.print() throws IllegalArgumentException on large BigDecima Summary: Correct the wrong calculation of "precision" in certain circumstances. Reviewed-by: darcy, alanb
jdk/src/share/classes/java/util/Formatter.java
jdk/test/java/util/Formatter/Basic-X.java
jdk/test/java/util/Formatter/Basic.java
jdk/test/java/util/Formatter/BasicBigDecimal.java
jdk/test/java/util/Formatter/BasicBigInteger.java
jdk/test/java/util/Formatter/BasicBoolean.java
jdk/test/java/util/Formatter/BasicBooleanObject.java
jdk/test/java/util/Formatter/BasicByte.java
jdk/test/java/util/Formatter/BasicByteObject.java
jdk/test/java/util/Formatter/BasicChar.java
jdk/test/java/util/Formatter/BasicCharObject.java
jdk/test/java/util/Formatter/BasicDateTime.java
jdk/test/java/util/Formatter/BasicDouble.java
jdk/test/java/util/Formatter/BasicDoubleObject.java
jdk/test/java/util/Formatter/BasicFloat.java
jdk/test/java/util/Formatter/BasicFloatObject.java
jdk/test/java/util/Formatter/BasicInt.java
jdk/test/java/util/Formatter/BasicIntObject.java
jdk/test/java/util/Formatter/BasicLong.java
jdk/test/java/util/Formatter/BasicLongObject.java
jdk/test/java/util/Formatter/BasicShort.java
jdk/test/java/util/Formatter/BasicShortObject.java
jdk/test/java/util/Formatter/genBasic.sh
--- a/jdk/src/share/classes/java/util/Formatter.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/src/share/classes/java/util/Formatter.java	Thu Jan 22 20:29:54 2009 -0800
@@ -39,6 +39,7 @@
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.math.MathContext;
+import java.math.RoundingMode;
 import java.nio.charset.Charset;
 import java.text.DateFormatSymbols;
 import java.text.DecimalFormat;
@@ -1252,7 +1253,7 @@
  *     Double#toString(double)} respectively, then the value will be rounded
  *     using the {@linkplain java.math.BigDecimal#ROUND_HALF_UP round half up
  *     algorithm}.  Otherwise, zeros may be appended to reach the precision.
- *     For a canonical representation of the value,use {@link
+ *     For a canonical representation of the value, use {@link
  *     Float#toString(float)} or {@link Double#toString(double)} as
  *     appropriate.
  *
@@ -3569,15 +3570,23 @@
                 // Create a new BigDecimal with the desired precision.
                 int prec = (precision == -1 ? 6 : precision);
                 int scale = value.scale();
-                int compPrec = value.precision();
-                if (scale > prec)
-                    compPrec -= (scale - prec);
-                MathContext mc = new MathContext(compPrec);
-                BigDecimal v
-                    = new BigDecimal(value.unscaledValue(), scale, mc);
-
-                BigDecimalLayout bdl
-                    = new BigDecimalLayout(v.unscaledValue(), v.scale(),
+
+                if (scale > prec) {
+                    // more "scale" digits than the requested "precision
+                    int compPrec = value.precision();
+                    if (compPrec <= scale) {
+                        // case of 0.xxxxxx
+                        value = value.setScale(prec, RoundingMode.HALF_UP);
+                    } else {
+                        compPrec -= (scale - prec);
+                        value = new BigDecimal(value.unscaledValue(),
+                                               scale,
+                                               new MathContext(compPrec));
+                    }
+                }
+                BigDecimalLayout bdl = new BigDecimalLayout(
+                                           value.unscaledValue(),
+                                           value.scale(),
                                            BigDecimalLayoutForm.DECIMAL_FLOAT);
 
                 char mant[] = bdl.mantissa();
--- a/jdk/test/java/util/Formatter/Basic-X.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/Basic-X.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1054,6 +1054,52 @@
         test("%4.1f", " 1.0", val);
         test("%4.2f", "0.99", val);
         test("%4.3f", "0.990", val);
+
+        // #6476425
+        val = new BigDecimal("0.00001");
+        test("%.0f", "0", val);
+        test("%.1f", "0.0", val);
+        test("%.2f", "0.00", val);
+        test("%.3f", "0.000", val);
+        test("%.4f", "0.0000", val);
+        test("%.5f", "0.00001", val);
+
+        val = new BigDecimal("1.00001");
+        test("%.0f", "1", val);
+        test("%.1f", "1.0", val);
+        test("%.2f", "1.00", val);
+        test("%.3f", "1.000", val);
+        test("%.4f", "1.0000", val);
+        test("%.5f", "1.00001", val);
+
+        val = new BigDecimal("1.23456");
+        test("%.0f", "1", val);
+        test("%.1f", "1.2", val);
+        test("%.2f", "1.23", val);
+        test("%.3f", "1.235", val);
+        test("%.4f", "1.2346", val);
+        test("%.5f", "1.23456", val);
+        test("%.6f", "1.234560", val);
+
+        val = new BigDecimal("9.99999");
+        test("%.0f", "10", val);
+        test("%.1f", "10.0", val);
+        test("%.2f", "10.00", val);
+        test("%.3f", "10.000", val);
+        test("%.4f", "10.0000", val);
+        test("%.5f", "9.99999", val);
+        test("%.6f", "9.999990", val);
+
+
+        val = new BigDecimal("1.99999");
+        test("%.0f", "2", val);
+        test("%.1f", "2.0", val);
+        test("%.2f", "2.00", val);
+        test("%.3f", "2.000", val);
+        test("%.4f", "2.0000", val);
+        test("%.5f", "1.99999", val);
+        test("%.6f", "1.999990", val);
+
 #end[BigDecimal]
 
 #if[float]
--- a/jdk/test/java/util/Formatter/Basic.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/Basic.java	Thu Jan 22 20:29:54 2009 -0800
@@ -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
+ *      6344623 6369500 6534606 6282094 6286592 6476425
  *
  * @run shell/timeout=240 Basic.sh
  */
--- a/jdk/test/java/util/Formatter/BasicBigDecimal.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicBigDecimal.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1055,6 +1055,52 @@
         test("%4.2f", "0.99", val);
         test("%4.3f", "0.990", val);
 
+        // #6476425
+        val = new BigDecimal("0.00001");
+        test("%.0f", "0", val);
+        test("%.1f", "0.0", val);
+        test("%.2f", "0.00", val);
+        test("%.3f", "0.000", val);
+        test("%.4f", "0.0000", val);
+        test("%.5f", "0.00001", val);
+
+        val = new BigDecimal("1.00001");
+        test("%.0f", "1", val);
+        test("%.1f", "1.0", val);
+        test("%.2f", "1.00", val);
+        test("%.3f", "1.000", val);
+        test("%.4f", "1.0000", val);
+        test("%.5f", "1.00001", val);
+
+        val = new BigDecimal("1.23456");
+        test("%.0f", "1", val);
+        test("%.1f", "1.2", val);
+        test("%.2f", "1.23", val);
+        test("%.3f", "1.235", val);
+        test("%.4f", "1.2346", val);
+        test("%.5f", "1.23456", val);
+        test("%.6f", "1.234560", val);
+
+        val = new BigDecimal("9.99999");
+        test("%.0f", "10", val);
+        test("%.1f", "10.0", val);
+        test("%.2f", "10.00", val);
+        test("%.3f", "10.000", val);
+        test("%.4f", "10.0000", val);
+        test("%.5f", "9.99999", val);
+        test("%.6f", "9.999990", val);
+
+
+        val = new BigDecimal("1.99999");
+        test("%.0f", "2", val);
+        test("%.1f", "2.0", val);
+        test("%.2f", "2.00", val);
+        test("%.3f", "2.000", val);
+        test("%.4f", "2.0000", val);
+        test("%.5f", "1.99999", val);
+        test("%.6f", "1.999990", val);
+
+
 
 
 
--- a/jdk/test/java/util/Formatter/BasicBigInteger.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicBigInteger.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicBoolean.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicBoolean.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicBooleanObject.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicBooleanObject.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicByte.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicByte.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicByteObject.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicByteObject.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicChar.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicChar.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicCharObject.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicCharObject.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicDateTime.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicDateTime.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicDouble.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicDouble.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1074,6 +1074,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %f - float, double, Double, BigDecimal
         //---------------------------------------------------------------------
--- a/jdk/test/java/util/Formatter/BasicDoubleObject.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicDoubleObject.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1074,6 +1074,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %f - float, double, Double, BigDecimal
         //---------------------------------------------------------------------
--- a/jdk/test/java/util/Formatter/BasicFloat.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicFloat.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1057,6 +1057,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %f - float
         //---------------------------------------------------------------------
--- a/jdk/test/java/util/Formatter/BasicFloatObject.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicFloatObject.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1090,6 +1090,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %g
         //
--- a/jdk/test/java/util/Formatter/BasicInt.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicInt.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicIntObject.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicIntObject.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicLong.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicLong.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicLongObject.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicLongObject.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicShort.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicShort.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/BasicShortObject.java	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/BasicShortObject.java	Thu Jan 22 20:29:54 2009 -0800
@@ -1524,6 +1524,52 @@
 
 
 
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %t
         //
--- a/jdk/test/java/util/Formatter/genBasic.sh	Tue Jan 20 16:16:35 2009 -0800
+++ b/jdk/test/java/util/Formatter/genBasic.sh	Thu Jan 22 20:29:54 2009 -0800
@@ -23,14 +23,14 @@
 # have any questions.
 #
 
-SPP='sh ../../../../make/java/nio/spp.sh'
+javac -d . ../../../../make/tools/src/build/tools/spp/Spp.java
 
 gen() {
 #  if [ $3 = "true" ]
 #  then $SPP -K$1 -Dtype=$1 -DType=$2 -Kprim<Basic-X.java >Basic$2.java
 #  else $SPP -K$1 -Dtype=$1 -DType=$2 -K$3 <Basic-X.java >Basic$2.java
 #  fi
- $SPP -K$1 -Dtype=$1 -DType=$2 -K$3 -K$4 -K$5 -K$6 <Basic-X.java >Basic$2.java
+    java build.tools.spp.Spp -K$1 -Dtype=$1 -DType=$2 -K$3 -K$4 -K$5 -K$6 <Basic-X.java >Basic$2.java
 }
 
 gen boolean Boolean       prim  ""  ""   ""
@@ -54,3 +54,5 @@
 gen BigDecimal BigDecimal ""    fp  ""   ""
 
 gen Calendar DateTime     ""    ""  ""   datetime
+
+rm -rf build