Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-3171

Handle floating conversion to BigDecimal and Double using 'as' gracefully.

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.5.7, 1.6-beta-2
    • 1.6-rc-2
    • None
    • None

    Description

      The current implementation of DGM.toBigDecimal uses a new BigDecimal(Number.doubleValue()) to do the conversion. As the docs on BigDecimal(double) explain, that may result in a bunch of insignificant digits yielding a decimal number that is not what the user expects (or wants).

      http://java.sun.com/javase/6/docs/api/java/lang/Double.html#toString(double)

      That means we get this:

      println (0.1f as BigDecimal)
      ==>
      0.100000001490116119384765625

      So the preferred method is to use new BigDecimal(Double.toString()) which will round off insignificant digits.

      So that now we get this:

      println (0.1f as BigDecimal)
      ==>
      0.1

      and most reassuringly this is now true (which fails in 1.5.x):

      assert (0.1 == (0.1f as BigDecimal))

      Attachments

        Activity

          People

            jimwhite James P. White
            jimwhite James P. White
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: