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

Wrong behavior of DecimalFormat with RoundingMode.UP in special case

XMLWordPrintable

    • Fix Understood
    • generic
    • generic

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.14393]

      A DESCRIPTION OF THE PROBLEM :
      When using java.text.DecimalFormat with RoundingMode.UP there is the following special case in which rounding does produce a wrong result:
       
      I try to format the double value 0.0001 with RoundingMode UP to a String rounded to two decimal place.

      As result I expect the String "0,01" but I get the String "0,00"

      When I format the double value 1.0001 with the same options I get the String "1.01" as expected when rounding to two decimal places with RoundingMode UP.

      The first case I mentioned is wrong behaviour.
      UP RoundingMode must not produce a String representing a double value that is smaller than the input double value. The javadoc of RoundingMode.UP says " Note that this rounding mode never decreases the magnitude of the calculated value." which is violated by the special case mentioned above.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Code example that shows the bevaviour:

      double dVal = 0.0001;
      DecimalFormat df = new DecimalFormat();
      df.setRoundingMode(RoundingMode.UP);
      df.applyPattern("0.00");
      String result = df.format(dVal));

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      result should be "0,01"
      ACTUAL -
      result is "0,00"

      REPRODUCIBILITY :
      This bug can be reproduced always.

            jlu Justin Lu
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: