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

DecimalFormat - rounding behaviour different from Java 6 and 7

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Duplicate
    • Affects Version/s: 8u45
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_2012

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.3.9600]
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      Rounding a double with DecimalFormat.format() can produce different results in Java 8 compared to previous major versions of Java.

      I've specifically tested with round mode set to RoundingMode.HALF_EVEN.

      An example number that formats differently between Java versions is 0.075 where you wish to round to 2 d.p.

      Java 7 will format to 0.8, Java 8 will format to 0.7

      I believe this is because the internal representation of the double 0.075 is 0.0749999999999999972244..... even though it displays as 0.075 if outputted to console.

      REGRESSION. Last worked in version 7u75

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile attached test case.
      Run the attached test case against a Java 6 or 7 VM.
      Then re-run the test case with Java 8 VM.

      Test case creates a double with value 0.075, creates a decimalformat set to max dp of 2 and outputs result of calling format() (with double passed in) to the console.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expected the output when running against a Java 8 VM to be the same as when run against Java 7 VM.

      i.e. Output in either case to be;

      Double: 0.075
      Fomatted D: 0.08
      ACTUAL -
      On java 7;

      Double: 0.075
      Fomatted D: 0.08

      On Java 8;

      Double: 0.075
      Fomatted D: 0.07


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class DecimalFormatBug
      {
      public static void main(String[] args)
      {
      double d = 0.075;

      DecimalFormat quantityFormat = new DecimalFormat();
      quantityFormat.setRoundingMode(RoundingMode.HALF_EVEN);
      quantityFormat.setMaximumFractionDigits(2);

      System.out.println("Double:\t\t" + d);
      System.out.println("Fomatted D:\t" + quantityFormat.format(d));
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Use Big Decimals instead of doubles.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                peytoia Yuka Kamiya (Inactive)
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: