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

java.math.BigInteger's setScale(int i) gives exception unexpectedly

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

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

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      if we have a BigDecimal with 3 or more digits after the decimal point, and we call
      the setScale method with 2 , then it gives exception.

      See example below

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      given the example no exception should be generated .
      a default Rounding mode should be used inside the method.
      ACTUAL -
      exception is generated unexpectadly

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------



      import java.math.BigDecimal;

      public class BigDecimalSetScale {

      public static void main(String[] args) {
      BigDecimal epoSacAmt0 = new BigDecimal("1.340") ;
      BigDecimal one = new BigDecimal("0.001") ;

      for (int i=0; i < 10 ; i ++){
      try {
      epoSacAmt0 = epoSacAmt0.add(one);
      epoSacAmt0.setScale(2);
      }catch(Exception e){
      System.out.println("exception " +e+" with "+epoSacAmt0);
      }

      }
      }


      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      use setScale(int, roundingMode) 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: