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

[Fmt-Nu] Rounding error of class java.text.DecimalFormat with #0.00 pattern

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Not an Issue
    • Affects Version/s: 1.4.2
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_2000

      Description

      FULL PRODUCT VERSION :
      Under Windows2000Professional: java version "1.4.2_02"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
      Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)

      Under HP-UX: Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1.10-030630-10:50)
      Java HotSpot(TM) Server VM (build 1.3.1 1.3.1.10-_30_jun_2003_11_52 PA2.0, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows 2000 [Version 5.00.2195] (checked bug with java.version=1.4.2_02)
      HP-UX B.11.11 U 9000/800 1113474677 (checked bug with java.runtime.version=1.3.1.10-030630-10:50)

      A DESCRIPTION OF THE PROBLEM :
      The class java.text.DecimalFormat with #0.00 pattern fail rounding.
      It fails when the centesimal number is par, is correct otherwise (odd).


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Make instace of java.text.DecimalFormat("#0.00") and then try to round the double value 0.025 (or every other number with decimal number par, i.e 0.045 0.065 ...)


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
          n=0.024 : 0.02
          n=0.025 : 0.03 <== see here
          n=0.026 : 0.03
          ...
          ...
          n=0.034 : 0.03
          n=0.035 : 0.04
          n=0.036 : 0.04
          
      ACTUAL -
          n=0.024 : 0.02
          n=0.025 : 0.02 <== MUST BE 0.3
          n=0.026 : 0.03
          ...
          ...
          n=0.034 : 0.03
          n=0.035 : 0.04 <== IS OK!
          n=0.036 : 0.04
          

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.text.DecimalFormat;

      /**
       *
       * @author Daniele Belli <a href=mailto:###@###.###>Daniele Belli</a>
       * The pourpose of this class is to show a bug of DecimalFormat class.<br>
       * This tester class print a table of number from 0.000 to 0.999 and its rounded number to two decimal from 0.00 to 1.00<br>
       * using #0.00 format string.<br>
       *
       * The bug is in rounding (for example): <br>
          n=0.024 : 0.02 <br>
          n=0.025 : 0.02 <== MUST BE 0.3 <br>
          n=0.026 : 0.03 <br>
          ... <br>
          ... <br>
          n=0.034 : 0.03 <br>
          n=0.035 : 0.04 <== IS OK! <br>
          n=0.036 : 0.04 <br><br>
          
          Seems that with a decimal number odd the round of number hundredth is correct.<br>
          Instead of with a par decimal number the round fails.
          
       */

      public class DecimalFormatTester {
          
          public DecimalFormatTester() {
              
              int multiplier = 1000;
              double n=0;
              
              DecimalFormat df=new DecimalFormat("#0.00");
              
              for (int i=1 ; i<multiplier ; i++) {
                  
                  n = i/ ((double) multiplier) ;
                  System.out.println("n=" + n + "\t : " + df.format(n) );
              }

          }
          
          public static void main (String args[]) {
              System.getProperties().list(System.out);
              new DecimalFormatTester();
          }
      }


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

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                peytoia Yuka Kamiya (Inactive)
                Reporter:
                okutsu Masayoshi Okutsu
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: