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

BigDecimal.valueof(double) cannot convert double to BigDecimal when it ends with 0 at 8th place and above

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 7, jre 1.8

      I think this issue might be with earlier version or later version

      A DESCRIPTION OF THE PROBLEM :
      Double number ending with 0 at 8th and beyond cannot be converted to BigDecimal with BigDecimal.valueOf method.

      12345670, 123456700, 123456780, 12345670.00 etc.
      e.g. double d = 12345670 becomes 1.234567e7
      BigDecimal bd = BigDecimal.valueOf(d) still remains 1.234567e7

      Later, if BigDecimal is set to preparedStatement, it turns to 0.

      e.g. preparedStatement.setBigDecimal(bd) //becomes 0.

      Workaround,
      It works if scale is set after converting to bigDecimal like,
      BigDecimal bd = BigDecimal.valueOf(d).setScale(any scale, any Rounding mode)

      I think, for usage purpose, plain valueOf should also convert the double to BigDecimal irrespective of the value.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. assign 12345670 to double // double is shown as scientific exponential for numbers ending with 0 at 8th and beyond place with or without non-significant decimals
      2. convert 12345670 to BigDecimal with method BigDecimal.valueOf //BigDecimal is still exponential

      Other issue, PreparedStatement can't handle such BigDecimal and converts the exponential BigDecimal to 0.


      public static vod main(String[] args){
      double d = 12345670; //1.234567e7
      BigDecimal bd = BigDecimal.valueOf(d); //bd is still 1.234567e7

      }



      Other numbers which has


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      12345670
      ACTUAL -
      1.234567e7

      ---------- BEGIN SOURCE ----------
      public static void main(String[] args) {
              Double d= 12345670;

              BigDecimal bd= BigDecimal.valueOf(d);
              System.out.println(bd);
              }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      public static void main(String[] args) {
              Double d= 12345670;

              BigDecimal bd= BigDecimal.valueOf(d).setScale(1, RoundingMode.HALF_UP);
              System.out.println(bd);
              }

      FREQUENCY : always


        Attachments

          Activity

            People

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

              Dates

              • Created:
                Updated:
                Resolved: