-2

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

pasin
  • 1
  • 1
  • 1
    `4.0027` will display as `4.00` if you echo it using number_format() with a precision of 2, or round it to 2 decimal places somewhere in your code – Mark Baker Dec 22 '14 at 08:16

2 Answers2

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