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

BigDecimal divide() calculates the wrong amount when RoundMode.UP is used.

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      linux_ubuntu

      Description

      A DESCRIPTION OF THE PROBLEM :
      The issue only happens when the quotient as a negative scale.
      In that case, the values of the quotient and dividend seems to be ignored, and:
      10^ |scale| is returned when RoundingMode.UP is used.
      0 is returned when RoundingMode.HALF_EVENis used.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      BigDecimal quotient = new BigDecimal("300").setScale(-2,RoundingMode.UP);
      BigDecimal dividend = new BigDecimal("100");
      System.out.println(quotient.divide(dividend,RoundingMode.UP).toPlainString());


      ---------- BEGIN SOURCE ----------
      package com.melo;

      import java.math.BigDecimal;
      import java.math.RoundingMode;

      public class BigDecimalTester {
      public static void main(String[] args) {
      BigDecimal quotient = new BigDecimal("300").setScale(-2, RoundingMode.UP);
      BigDecimal dividend = new BigDecimal("100");
      System.out.println(quotient.divide(dividend, RoundingMode.UP).toPlainString());
      System.out.println(quotient.divide(dividend).toPlainString());
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Do not specify a RoundingMode seems to avoid the issue

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              bpb Brian Burkhalter
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: