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?
The scale constraint allows you to control this:
...
BigDecimal myNum
static constraints = {
myNum(scale: 6)
}
...
http://grails.org/doc/latest/ref/Constraints/scale.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With