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

(fmt) Formatter.format("%4.0f", new BigDecimal("99.95")) throws ArrayIndexOutOfBounds

    • b91
    • x86
    • windows_xp

      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.

            iris Iris Clark
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: