存储在MySQL中的浮点值不断变得混乱 [英] Float value stored in MySQL keeps getting messed up
问题描述
我试图在MySQL中存储浮点值,我的值似乎不断变得混乱。 :(
$ b $我有我的领域定义为浮动(10,7)
和我在我的值正确的PHP插入之前:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' / code>
但是当我插入一个值如47.5206797,它在我的表中显示为47.520679 5 。为什么会这样?
如果您需要精确的值,请将其存储为精确的数据类型,如 DECIMAL(17,7)
,它将提供与 FLOAT(10,7)
相同的范围。唯一的缺点是那么 DECIMAL
会占用比equivilant FLOAT
更多的磁盘空间,但是与校正浮点数相比,这是微不足道的错误,其中精度是一个问题。
http://dev.mysql.com/doc/refman/5.0/en/fixed-point-types.html
佛有关浮点数问题的更多信息,以下内容可能值得一读
http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
I'm trying to store float values in MySQL and my values seem to keep getting messed up. :(
I have my fields defined as float(10,7)
and I round my values properly in PHP before inserting them:
$rndval = round($val,7)
INSERT INTO mytable (float) VALUES ($rndval)
But when I insert a value such as 47.5206797, it shows up as 47.5206795 in my table. Why is that?
If you need a value to be precise, store it as an exact data type such as DECIMAL(17,7)
, which would provide the same range as FLOAT(10,7)
. The only down side is that the DECIMAL
will take up more disk space than the equivilant FLOAT
, however this is trivial compared to correcting for floating point errors where precision is a concern.
http://dev.mysql.com/doc/refman/5.0/en/fixed-point-types.html
For more information on floating point number issues, the following may be worth a read
http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
这篇关于存储在MySQL中的浮点值不断变得混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!