In my app i want save float or double values that are calculated using php into mysql. I also want to make calculations with these columns in stored procedures using sql. But i notice that double and float data types show different in php and mysql. for example after a calculation in php i might get a value 4.00 while the same calculation is mysql is 4.0027. How can i make possible to have more or less the same value calclated. what type should my columns in mysql be and what cast should i make in php? Thanx
Asked
Active
Viewed 1,476 times
2 Answers
0
You may read this page for answers why sum of two integers often results in a float.
Considering the MySQL data type, the difference is discussed and explained in this question: Difference between float and decimal data type
I would suggest to store in decimal with some rounding before insert/update in PHP.
Again, everything depends on the calculations you perform on the values.

Community
- 1
- 1

Sergey Telshevsky
- 12,077
- 6
- 55
- 78
0
Try this
use number_format("4.0024",2);
output 4.00
or Update
use number_format("6556564.0024",2, '.', '');
or number_format(6556564.0024,2, '.', '');
output 6556564.00

Ahosan Karim Asik
- 3,219
- 1
- 18
- 27
-
yes but gives me back a string result. I still want a number with 2 decimal places – pasin Dec 22 '14 at 13:41