Mikey
Mikey

Reputation: 4742

Grails 2.0 trouble with persisting BigDecimal

I am trying to persist a BigDecimal in a brand new grails 2.0 app, and it's not behaving at all how I am expecting.

I make a new app called l2bigdec and add this domain class:

package l2bigdec

class PlayMe {
  BigDecimal imStupidOrSomething
  static constraints = {
  }
}

Then I put this code in the 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 = {
  }
}

Which prints:

0.912345
0.91

Why is it not printing 0.912345 both times? Do I not understand BigDecimal?

Upvotes: 5

Views: 2096

Answers (1)

Andrew
Andrew

Reputation: 2249

The scale constraint allows you to control this:

...
BigDecimal myNum

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

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

Upvotes: 10

Related Questions