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

Decimal format doesn't round a double properly when the number is less than 1

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.2.1, 1.2.2, 1.3.0
    • core-libs
    • beta2
    • x86
    • windows_98, windows_nt



      Name: skT88420 Date: 05/27/99


      import java.text.*;

      class fmt
      {
         public static void main (String args[])
         {
            System.out.println("Why does a value of .019 give 2% when a value of .009 gives 0%?\n");

            double d1 = .019;
            double d2 = .009;
            double d3 = .015;
            double d4 = .016;
            double d5 = .014;
            double d6 = .004;
            double d7 = .005;
            double d8 = .006;
            double d9 = .007;
            double d10 = .008;

            NumberFormat f1 = new DecimalFormat("##0%");

            System.out.println( d1 + " = " + f1.format(d1)); // should be 2%
            System.out.println( d2 + " = " + f1.format(d2)); // should be 1%
            System.out.println( d3 + " = " + f1.format(d3));
            System.out.println( d4 + " = " + f1.format(d4));
            System.out.println( d5 + " = " + f1.format(d5));
            System.out.println( d6 + " = " + f1.format(d6));
            System.out.println( d7 + " = " + f1.format(d7));
            System.out.println( d8 + " = " + f1.format(d8));
            System.out.println( d9 + " = " + f1.format(d9));
            System.out.println( d10 + " = " + f1.format(d10 ));

            System.out.println("\nI would have assumed a 1%");
         }
      }


      //E:\testjava\project16>java -version
      //java version "1.2.1"
      //HotSpot VM (1.0fcs, mixed mode, build E)

      //E:\testjava\project16>java -fullversion
      //java full version "JDK-1.2.1-A"
      (Review ID: 83580)
      ======================================================================

      Name: skT88420 Date: 10/01/99


      java.text.DecimalFormat rounds doubles with 5 as a fraction digit inconsistently.

      For example if double 0.05 is formatted using "#,##0.0", the result will be 0.0 (expected 0.1). But double 0.15 is formatted correctly: 0.2.

      ------------- SOURCE CODE --------------------
      import java.text.*;

      public class Test {
        public static void main(String[] args) {
          format (0.5, "#,##0", "1");
          format (1.5, "#,##0", "2");
          format (0.05, "#,##0.0", "0.1");
          format (0.15, "#,##0.0", "0.2");
          format (0.005, "#,##0.00", "0.01");
          format (0.015, "#,##0.00", "0.02");
          format (0.0005, "#,##0.000", "0.001");
          format (0.0015, "#,##0.000", "0.002");
        }
          
        public static void format (double d,
                                   String format,
                                   String expected) {
          DecimalFormat df = new DecimalFormat (format);
          System.out.println ("\nformatting double: " + d +
                              " (format: " + format + ")" +
                              "\nresult: " + df.format(d) +
                              " (expected: " + expected + ")");
        }
      }
      ------------- END OF SOURCE ---------------------------

      ------------- OUTPUT ---------------------------
      formatting double: 0.5 (format: #,##0)
      result: 0 (expected: 1)

      formatting double: 1.5 (format: #,##0)
      result: 2 (expected: 2)

      formatting double: 0.05 (format: #,##0.0)
      result: 0.0 (expected: 0.1)

      formatting double: 0.15 (format: #,##0.0)
      result: 0.2 (expected: 0.2)

      formatting double: 0.0050 (format: #,##0.00)
      result: 0.00 (expected: 0.01)

      formatting double: 0.015 (format: #,##0.00)
      result: 0.02 (expected: 0.02)

      formatting double: 5.0E-4 (format: #,##0.000)
      result: 0.000 (expected: 0.001)

      formatting double: 0.0015 (format: #,##0.000)
      result: 0.002 (expected: 0.002)
      -------------- END OF OUTPUT --------------------

      OUTPUT OF java -version:
      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      OUTPUT OF java -fullversion:
      JAVA.EXE full version "JDK-1.2.2-W"
      (Review ID: 96002)
      ======================================================================

            nlindenbsunw Norbert Lindenberg (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: