United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6476425 (fmt) java.util.Formatter.print() throws IllegalArgumentException on large BigDecimal
JDK-6476425 : (fmt) java.util.Formatter.print() throws IllegalArgumentException on large BigDecimal

Details
Type:
Bug
Submit Date:
2006-09-28
Status:
Resolved
Updated Date:
2011-02-16
Project Name:
JDK
Resolved Date:
2009-01-31
Component:
core-libs
OS:
solaris_10
Sub-Component:
java.util
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
6u9
Fixed Versions:

Related Reports
Backport:
Relates:
Relates:

Sub Tasks

Description
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.

                                    

Comments
EVALUATION

Minor error in determining the requested precision.
                                     
2006-09-29
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());
     }
 }
                                     
2009-01-13
EVALUATION

need backport to 6u14.
                                     
2009-01-26



Hardware and Software, Engineered to Work Together