Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2172045 | 6u14 | Xueming Shen | P3 | Resolved | Fixed | b02 |
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.
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.
- backported by
-
JDK-2172045 (fmt) java.util.Formatter.print() throws IllegalArgumentException on large BigDecimal
- Resolved
- relates to
-
JDK-6369500 (fmt) Formatter.format("%4.0f", new BigDecimal("99.95")) throws ArrayIndexOutOfBounds
- Resolved
-
JDK-6796662 (fmt spec) Formatter spec on BigDecimal output should not reference Float and Double
- Closed