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

DecimalFormat.format() truncating output

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6u26
    • core-libs
    • x86
    • windows_vista

      FULL PRODUCT VERSION :
      java version "1.6.0_24"
      Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
      Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]
      Also verified on Mac OS X 10.6.8

      A DESCRIPTION OF THE PROBLEM :
      The DecimalFormat class returns inconsistent values when calling the format(double number) method.

      According to the documentation for DecimalFormat: "#,##0.0#;(#)" produces precisely the same behavior as "#,##0.0#;(#,##0.0#)"

      However, this is not the case. The output produced by the former truncates the closing parenthesis for negative values. The latter appears to work correctly.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the following JUnit test method to reproduce the problem:

      @Test
          public void testDecimalFormat() {
              double value = -4000d;

              final String expected = "(4,000.00)";
              final String actualA = new DecimalFormat("#,##0.00;(#,##0.00)").format(value);
              final String actualB = new DecimalFormat("#,##0.00;(#)").format(value);

              assertEquals(expected, actualA);
              assertEquals(expected, actualB);
          }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Both assertions should succeed, as the output should be identical as per the documentation.
      ACTUAL -
      The assertion on actualB fails as the trailing parenthesis is missing:

      Expected :(4,000.00)
      Actual :(4,000.00

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package formatting;

      import org.junit.Test;

      import java.text.DecimalFormat;

      import static org.junit.Assert.assertEquals;

      public class FormatTest {

          @Test
          public void testDecimalFormat() {
              double value = -4000d;

              final String expected = "(4,000.00)";
              final String actualA = new DecimalFormat("#,##0.00;(#,##0.00)").format(value);
              final String actualB = new DecimalFormat("#,##0.00;(#)").format(value);

              assertEquals(expected, actualA);
              assertEquals(expected, actualB);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The format must be fully specified (i.e. duplicated) for both the positive and negative patterns in order for this to work until a bug fix is released.

            peytoia Yuka Kamiya (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: