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

BigDecimal inaccurate rounding with Rounding.HALF_UP

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      os_x

      Description

      ADDITIONAL SYSTEM INFORMATION :
      OSX, JDK 12,8

      A DESCRIPTION OF THE PROBLEM :
      Wrong rounding :
                  double value = .0002755d;
              BigDecimal bd = new BigDecimal(value);
              bd = bd.setScale(6, RoundingMode.HALF_UP);
              System.out.println(String.format("%.7f", bd.doubleValue()));
              

      is returning:
      0.0002750
      should be
      0.0002760


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
              double value = .0002755d;
              BigDecimal bd = new BigDecimal(value);
              bd = bd.setScale(6, RoundingMode.HALF_UP);
              System.out.println(String.format("%.7f", bd.doubleValue()));

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      0.0002760
      ACTUAL -
      0.0002750

      ---------- BEGIN SOURCE ----------
              double value = .0002755d;
              BigDecimal bd = new BigDecimal(value);
              bd = bd.setScale(6, RoundingMode.HALF_UP);
              System.out.println(String.format("%.7f", bd.doubleValue()));
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      System.out.println(String.format("%.6f", value));

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            psonal Pallavi Sonal (Inactive)
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: