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

java.text.DecimalFormat.format behaves inconsistently

    XMLWordPrintable

Details

    • beta
    • sparc
    • solaris_2.5

    Description



      Name: dfC67450 Date: 03/05/99


      java.text.DecimalFormat.format behaves inconsistently in formatting
      fraction part. 0.001 and 1.001 are formatted in a different way.

      Please note that javadoc doesn't specify which one is correct.

      Here is the test demonstrating the bug:

      -----------------Test.java------------------------
      import java.text.*;

      public class Test {
          public static void main (String args[]){
              double d0 = 0.001;
              double d1 = 1.001;
              DecimalFormat df = new DecimalFormat();
              df.setMaximumFractionDigits(2);
              String formatted0 = df.format(d0);
              String formatted1 = df.format(d1);
              System.out.println("pattern: " + df.toPattern());
              System.out.println(d0 + " formatted as : " + formatted0);
              System.out.println(d1 + " formatted as : " + formatted1);
              if (formatted0.length() == formatted1.length())
                  System.out.println("Test passed");
              else
                  System.out.println("Test failed");
          }
      }

      ---------Output from the test---------------------
      pattern: #,##0.##
      0.0010 formatted as : 0
      1.001 formatted as : 1.00
      Test failed
      --------------------------------------------------

      ======================================================================

      Attachments

        Activity

          People

            nlindenbsunw Norbert Lindenberg (Inactive)
            fdasunw Fda Fda (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: