Grails 2.0 error with BigDecimal persistence

I am trying to save BigDecimal in a new Grails 2.0 application and it does not behave at all as I expect.

I create a new application called l2bigdecand add this domain class:

package l2bigdec

class PlayMe {
  BigDecimal imStupidOrSomething
  static constraints = {
  }
}

Then I put this code in bootstrap:

import l2bigdec.*
class BootStrap {

  def init = { servletContext ->
    def thisThingIHate =  new PlayMe(imStupidOrSomething:0.912345).save(failOnError:true)
    println thisThingIHate.imStupidOrSomething
    PlayMe.withSession{it.clear()}
    def getItBack = PlayMe.find{it}
    println getItBack.imStupidOrSomething
  }
  def destroy = {
  }
}

What prints:

0.912345
0.91

Why doesn't he print 0.912345 both times? Don't I understand BigDecimal?

+5
source share
1 answer

Scale limitation allows you to control this:

...
BigDecimal myNum

static constraints = {
   myNum(scale: 6)
}
...

http://grails.org/doc/latest/ref/Constraints/scale.html

+10
source

All Articles