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 asFLOAT(10,7). The only down side is that theDECIMALwill take up more disk space than the equivilantFLOAT, 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