Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 625
Mikey Avatar asked Feb 25 '12 00:02

Mikey


1 Answers

The scale constraint allows you to control this:

...
BigDecimal myNum

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

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

like image 154
Andrew Avatar answered Oct 22 '22 21:10

Andrew