Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6476425

(fmt) java.util.Formatter.print() throws IllegalArgumentException on large BigDecimal

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 7
    • 6u9
    • core-libs
    • b46
    • generic
    • solaris_10

        It seems that there is a bug, most likely in java.util.Formatter.print(StringBuilder sb, BigDecimal value, Locale l, Flags f, char c, int precision, boolean neg), which will throw an IllegalArgumentException when printing certain java.math.BigDecimal values whose scales are sufficiently large. In particular, the following program will throw an exception when it should not:
         
        import java.math.BigDecimal;
         
        /**
         * Illustrates a bug in formatting BigDecimal objects.
         */
        public class BadFormatter
        {
            /**
             * Will throw an IllegalArgumentException, but should not.
             */
            public static void main(String[] args)
            {
                BigDecimal bd = new BigDecimal("0.0000");
                System.out.printf("%.2f", bd);
            }
        }
         
        ###@###.### ~> java BadFormatter
        Exception in thread "main" java.lang.IllegalArgumentException: Digits < 0
                at java.math.MathContext.<init>(MathContext.java:157)
                at java.math.MathContext.<init>(MathContext.java:141)
                at java.util.Formatter$FormatSpecifier.print(Formatter.java:3541)
                at java.util.Formatter$FormatSpecifier.print(Formatter.java:3460)
                at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2716)
                at java.util.Formatter$FormatSpecifier.print(Formatter.java:2664)
                at java.util.Formatter.format(Formatter.java:2430)
                at java.io.PrintStream.format(PrintStream.java:899)
                at java.io.PrintStream.printf(PrintStream.java:800)
                at BadFormatter.main(BadFormatter.java:14)
         
        Note that I tested this with Java 5 Update 7 (which produced the stack trace above), Java 5 Update 8, and Java 6 build 90, and found that all exhibited the bug. This was with Solaris 8 SPARC and Solaris 10 x86, but I don't think that's relevant -- I believe the bug will exhibit itself on every architecture since it's a problem with the java.util.Formatter class itself.

              sherman Xueming Shen
              place Peter Lace (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: