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

Mathematically wrong implementations of RoundingMode.HALF_UP and .HALF_DOWN

    XMLWordPrintable

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      any system - since this was intended, however it is not correct

      A DESCRIPTION OF THE PROBLEM :
      HALF_UP should mean that a half-point number needs to be rounded towards the bigger number
      1.5 rounded HALF_UP = 2 (this is implemented OK)
      -1.5 rounded HALF_UP should be rounded to the bigger value, that is -1
      => implementation o HALF_UP for negative numbers is wrong
      similarly,
      HALF_DOWN should mean that a half-point number needs to be rounded towards the smaller number
      1.5 rounded HALF_DOWN = 1 (this is implemented OK)
      -1.5 rounded HALF_DOWN should be rounded to the smaller value, that is -2
      => implementation o HALF_DOWN for negative numbers is wrong

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      any usage of the HALF_UP or HALF_DOWN is not working mathematically correct
      even the javadocs describe the desired behavior incorrectly => incorrect design

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      -1.5 rounded HALF_UP is: -1
      -1.5 rounded HALF_DOWN is:-2
      ACTUAL -
      -1.5 rounded HALF_UP is: -2
      -1.5 rounded HALF_DOWN is:-1

      ---------- BEGIN SOURCE ----------
      NumberFormat nf = NumberFormat.getInstance();
      nf.setMaximumFractionDigits(0);
      nf.setMinimumFractionDigits(0);

      nf.setRoundingMode(RoundingMode.HALF_UP);
      System.out.println("-1.5 rounded HALF_UP is: " + nf.format(-1.5));
      nf.setRoundingMode(RoundingMode.HALF_DOWN);
      System.out.println("-1.5 rounded HALF_DOWN is:" + nf.format(-1.5));
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              darcy Joe Darcy
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: