Best practice is don't store decimals. There are a few known problems with both MySql and PHP when dealing with floating point numbers. http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
Best practice is store the smallest unit and then math everything up rather than doing math on floating point numbers. I am not sure what you are storing but lets pretend it is price. Instead of storing price in dollars as a float store it as pennies and calculate dollars where needed. So rather than $price = 23.99 you want $price = 239900... when adding/multiplying/dividing prices you never have to worry about the rounding and floating..... do all calculations on real numbers (pennies or seconds or millimeters) and then do some math to get the dollar/hour/mile values.
I use decimal datatype in MySql for storing monetary values.
When displaying the output after a calculation I use the following:
// the following displays the calculated value to 2 decimal places
echo number_format($yourCalculatedValue, 2, '.', '');
// the following (note the fourth parameter) will display thousands separated by a comma
echo number_format($yourCalculatedValue, 2, '.', ',');
Thank you, I guess I'll just multiply by 100 before inserting, and divide by 100 when retrieving it. Thanks!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community