Mysql 浮点值未正确持久化到数据库
我有一个Grails应用程序,其中我正在解析一个XML文件,并使用解析后的数据创建对象并将它们持久化到MySQL数据库 我的一个类具有Mysql 浮点值未正确持久化到数据库,mysql,hibernate,grails,floating-point,Mysql,Hibernate,Grails,Floating Point,我有一个Grails应用程序,其中我正在解析一个XML文件,并使用解析后的数据创建对象并将它们持久化到MySQL数据库 我的一个类具有Float属性: class Foo { // ... Float myFloat static constraints = { myFloat(scale: 9) // Trying to specify 9 digits of precision, but this doesn't seem to be making
Float
属性:
class Foo {
// ...
Float myFloat
static constraints = {
myFloat(scale: 9) // Trying to specify 9 digits of precision, but this doesn't seem to be making any difference
}
// ...
}
在解析XML时,我找到了一个值为6378137
的属性。我想将该值分配给myFloat
:
class MyService{
// ...
def xml = new XmlParser().parseText(myXmlFile.getText())
def foo = new Foo(
myFloat: xml.attribute("my_float")?.toFloat()
).save()
// ...
}
此时在调试器中,我可以看到myFloat
的值是6378137.0
。问题是,在事务提交后,存储在数据库中的值是6378140
为什么不将
myFloat
与我分配给它的值一起存储?您必须在GORM/hibernate中设置精度和比例
查看下面的链接
基础列的数据类型是什么?@JamesKleeh数据类型是
float
。这让我对正在发生的事情有了一些了解,但我很难将您的答案翻译成Grails术语。我尝试过使用,但这似乎没有任何区别。有什么想法吗?