JDK-6369500 : (fmt) Formatter.format("%4.0f", new BigDecimal("99.95")) throws ArrayIndexOutOfBounds
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 5.0
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-01-06
  • Updated: 2012-01-11
  • Resolved: 2006-07-01
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.
JDK 6
6 b91Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
String.format throws an ArrayOutOfBoundsException with certain formats and BigDecimal values but other formats or values work fine.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
String.format( "%4.0f", new BigDecimal( "99.95" ) )

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
 100

ACTUAL -
java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:519)
	at java.lang.StringBuilder.append(StringBuilder.java:190)
	at java.util.Formatter$FormatSpecifier$BigDecimalLayout.layout(Formatter.java:3690)
	at java.util.Formatter$FormatSpecifier$BigDecimalLayout.<init>(Formatter.java:3610)
	at java.util.Formatter$FormatSpecifier.print(Formatter.java:3545)
	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.util.Formatter.format(Formatter.java:2364)
	at java.lang.String.format(String.java:2520)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.math.BigDecimal;

public class BigDecimalFormatExceptionDemo {
   public static void main(String[] args) {
      try {
         System.out.println( System.getProperty( "os.name") );
         System.out.println( System.getProperty( "os.version") );
         System.out.println( System.getProperty( "java.version") );
         BigDecimal v1 = new BigDecimal("99.00");

         System.out.println( String.format( "%4.2f", v1) );                   // OK
         System.out.println( String.format( "%4.2f", v1.doubleValue() ) );    // OK
         System.out.println( String.format( "%4.0f", v1.doubleValue() ) );    // OK
         System.out.println( String.format( "%4.0f", v1) );

         BigDecimal v2 = new BigDecimal("99.95");                             // OK
         System.out.println( String.format( "%4.2f", v2) );                   // OK
         System.out.println( String.format( "%4.2f", v2.doubleValue() ) );    // OK
         System.out.println( String.format( "%4.0f", v2.doubleValue() ) );    // OK
         System.out.println( String.format( "%4.0f", v2) );                   // Error
      }
      catch ( Throwable x ) {
         x.printStackTrace();
      }
   }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
String.format( "%4.0f", new BigDecimal( "99.95" ) )

generates the error but you can use

String.format( "%4.0f", new BigDecimal( "99.95" ).doubleValue() )

as a workaround.

Comments
EVALUATION We are apparently missing code to handle this case.
08-06-2006