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

DecimalFormat RoundingMode.HALF_UP is broken (HALF_EVEN is OK)

    Details

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

      Description

      FULL PRODUCT VERSION :
      java version "1.8.0_05"
      Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
      Java HotSpot(TM) Client VM (build 25.5-b02, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      When formatting with BigDecimal and a configured maximum number of fraction digits, the HALF_UP RoundingMode returns wrong results for some numbers. HALF_EVEN works as expected.

      Please note that this is NOT just the JDK8 rounding change as mentioned in the compatibility notes -- this rounding is fundamentally broken. In my example I have a smaller number rounding up, and the larger number rounding down.

      REGRESSION. Last worked in version 7u51

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.7.0_51"
      Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
      Java HotSpot(TM) Client VM (build 24.51-b03, mixed mode, sharing)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      As per source code:
      create a new DecimalFormat, set the maximum fraction digits, set rounding mode to half_up, and format various numbers.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      HALF_UP
      99.9989 -> 100
      99.999 -> 100
      HALF_EVEN
      99.9989 -> 100
      99.999 -> 100

      ACTUAL -
      HALF_UP
      99.9989 -> 100
      99.999 -> 99.99
      HALF_EVEN
      99.9989 -> 100
      99.999 -> 100


      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class Decimal {
          public static void main(String[] args) {
              DecimalFormat format = new DecimalFormat();
              format.setMaximumFractionDigits(2);
              format.setRoundingMode(RoundingMode.HALF_UP);
              System.out.println("HALF_UP");
              System.out.println(99.9989+" -> "+format.format(99.9989));
              System.out.println(99.9990 + " -> " + format.format(99.9990));
              format.setRoundingMode(RoundingMode.HALF_EVEN);
              System.out.println("HALF_EVEN");
              System.out.println(99.9989+" -> "+format.format(99.9989));
              System.out.println(99.9990+" -> "+format.format(99.9990));
          }
      }

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

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: