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

Modifications to NumberFormat format() caused new rounding errors

    XMLWordPrintable

Details

    • Bug
    • Resolution: Not an Issue
    • P4
    • None
    • 8u20
    • core-libs

    Description

      FULL PRODUCT VERSION :
      java version "1.8.0_20"
      Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
      Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Darwin opf.local 13.4.0 Darwin Kernel Version 13.4.0: Sun Aug 17 19:50:11 PDT 2014; root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      Changes to the fix the rounding behaviour of NumberFormat format() (see http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7131459) appear to have cause new failures in other edge cases.

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.7.0_25"
      Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Attempt to use HALF_EVEN rounding when printing 12.345 with two decimal places of accuracy. It will give 12.35 instead of 12.34.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      JVM Version: 1.8.0-ea
      Should print (3 sig.fig.) 0.805 got 0.805 - correct? = true
      Should print (2 sig.fig.) 12.34 got 12.35 - correct? = true

      ACTUAL -
      Under Java 7, I get this:

      JVM Version: 1.7.0_51
      Should print (3 sig.fig.) 0.805 got 0.806 - correct? = false
      Should print (2 sig.fig.) 12.34 got 12.34 - correct? = true

      But under Java 8:

      JVM Version: 1.8.0-ea
      Should print (3 sig.fig.) 0.805 got 0.805 - correct? = true
      Should print (2 sig.fig.) 12.34 got 12.35 - correct? = false


      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class TestNumberFormatRounding {

          public static void main(String[] args) {
              System.out
                      .println("JVM Version: " + System.getProperty("java.version"));

              NumberFormat nf = java.text.NumberFormat.getInstance();
              nf.setMaximumFractionDigits(3);
              nf.setMinimumFractionDigits(0);
              String expected = "0.805";
              String result = nf.format(0.8055d);
              System.out.println("Should print (3 sig.fig.) " + expected + " got "
                      + result
                      + " - correct? = " + (expected.equals(result)));

              nf.setMaximumFractionDigits(2);
              double test = 12.345;
              expected = "12.34";
              result = nf.format(test);
              System.out.println("Should print (2 sig.fig.) " + expected + " got "
                      + result
                      + " - correct? = " + (expected.equals(result)));
          }

      }
      ---------- END SOURCE ----------

      Attachments

        Issue Links

          Activity

            People

              olagneau Olivier Lagneau (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: