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

BigDecimal not handling Scientific Notation correctly

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 6u24
    • core-libs
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0_24"
      Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
      Java HotSpot(TM) Client VM (build 19.1-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]
      SP3

      A DESCRIPTION OF THE PROBLEM :
      BigDecimal seems to have trouble parsing some scientific notation strings when passed into the constructor.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create a BigDecimal by passing in a string to the constructor with a large exponent (eg. "4.0E+7").
      Create another BigDecimal by passing in a reasonably large number string (eg. "200.0")
      Divide the first BigDecimal by the second.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected Result: 200000.0
      ACTUAL -
      Actual result: 0.0

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.math.BigDecimal;

      public class JavaBigDecimalBug {
          public static void main(String[] args) {
              BigDecimal a1 = new BigDecimal("4.0E+7");
              BigDecimal b1 = new BigDecimal(Double.toString(200.0));
              BigDecimal c1 = a1.divide(b1, BigDecimal.ROUND_HALF_DOWN);
              System.out.println("a1 = " + a1);
              System.out.println("a1.doubleValue() = " + a1.doubleValue());
              System.out.println("b1 = " + b1);
              System.out.println("b1.doubleValue() = " + b1.doubleValue());
              System.out.println("c1 = " + c1);
              System.out.println("c1.doubleValue() = " + c1.doubleValue());

          }
      }


      Output:
      a1 = 4.0E+7
      a1.doubleValue() = 4.0E7
      b1 = 200.0
      b1.doubleValue() = 200.0
      c1 = 0E+6
      c1.doubleValue() = 0.0
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Workaround: pass in doubles instead of String representations.


      import java.math.BigDecimal;

      public class JavaBigDecimalBug {
          public static void main(String[] args) {
              BigDecimal a2 = new BigDecimal(4.0E+7);
              BigDecimal b2 = new BigDecimal(200.0);
              BigDecimal c2 = a2.divide(b2, BigDecimal.ROUND_HALF_DOWN);
              System.out.println("a2 = " + a2);
              System.out.println("a2.doubleValue() = " + a2.doubleValue());
              System.out.println("b2 = " + b2);
              System.out.println("b2.doubleValue() = " + b2.doubleValue());
              System.out.println("c2 = " + c2);
              System.out.println("c2.doubleValue() = " + c2.doubleValue());

          }
      }


      Output a2 = 40000000
      a2.doubleValue() = 4.0E7
      b2 = 200
      b2.doubleValue() = 200.0
      c2 = 200000
      c2.doubleValue() = 200000.0

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: