JDK-6476425 : (fmt) java.util.Formatter.print() throws IllegalArgumentException on large BigDecimal
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 6u9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_10
  • CPU: generic
  • Submitted: 2006-09-28
  • Updated: 2011-02-16
  • Resolved: 2009-01-31
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
6u14Fixed 7 b46Fixed
Related Reports
Relates :  
Relates :  
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.

EVALUATION need backport to 6u14.

WORK AROUND The usual workaround for this problem is to invoke BigDecimal.{double,float}Value() as follows: import java.math.BigDecimal; public class Test { public static void main(String [] args) { BigDecimal bd = new BigDecimal("0.0000"); System.out.format("%f%n", bd); System.out.format("%.2f%n", bd.doubleValue()); } }

EVALUATION Minor error in determining the requested precision.