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

RoundingMode.HALF_UP does not work as expected

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_91"
      Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      should not be neccessary

      A DESCRIPTION OF THE PROBLEM :
      when trying to use NumberFormat - 2 fraction-digits - HALF_ROUND_UP I expect that
      1.785 is rounded up to 1.79 - this works in Java <= 7
      Java 8 (and 9) round down - which is wrong


      REGRESSION. Last worked in version 7u76

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the example source code once with JVM7 and JVM8 - you should see the differences


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      JAVA 1.6.0_45 - OK
      "c:\Program Files\Java\jdk1.6.0_45\bin\java.exe" TestRoundUp
      1.785 ~ 1.79 (Expected: 1.79)
      1.795 ~ 1.80 (Expected: 1.80)

      JAVA 1.7.0_75 - OK
      "c:\Program Files (x86)\Java\jdk1.7.0_75\bin\java.exe" TestRoundUp
      1.785 ~ 1.79 (Expected: 1.79)
      1.795 ~ 1.80 (Expected: 1.80)
      ACTUAL -
      JAVA 1.8.0_40 - FAIL
      "c:\Program Files\Java\jdk1.8.0_40\bin\java.exe" TestRoundUp
      1.785 ~ 1.78 (Expected: 1.79)
      1.795 ~ 1.79 (Expected: 1.80)

      JAVA 1.8.0_91 - FAIL
      "c:\Program Files (x86)\Java\jre1.8.0_91\bin\java.exe" TestRoundUp
      1.785 ~ 1.78 (Expected: 1.79)
      1.795 ~ 1.79 (Expected: 1.80)

      JAVA 1.9 - FAIL
      "c:\Program Files\Java\jdk-9\bin\java.exe" TestRoundUp
      1.785 ~ 1.78 (Expected: 1.79)
      1.795 ~ 1.79 (Expected: 1.80)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.math.RoundingMode;
      import java.text.NumberFormat;
      import java.util.Locale;

      public class TestRoundUp {
      public static void main(final String[] args) {
      NumberFormat nf = NumberFormat.getInstance(Locale.US); // should not matter here

      nf.setMinimumFractionDigits(2);
      nf.setMaximumFractionDigits(2);
      nf.setRoundingMode(RoundingMode.HALF_UP);

      System.out.println("1.785 ~ " + nf.format(1.785) + " (Expected: 1.79)");
      System.out.println("1.795 ~ " + nf.format(1.795) + " (Expected: 1.80)");
      }
      }
      ---------- END SOURCE ----------

      SUPPORT :
      YES

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: