I'm storing monetary values in a mysql table as floats.
problem being, mysql is rounding the numbers up or down.
ex. 12345.68 gets rounded to 12345.7
How can I stop this or is there a better data type I should be using?
I would like to retain the original values within the 2 decimal places.
Do not use
FLOAT
type. UseDECIMAL
instead. Float converts decimal numbers to binary which results in rounding (loss of precision). Decimal stores the numbers as decimals - no conversion.In your case defining the column as
DECIMAL(12,2)
should be ok. Chose the width (first number) based on the expected size of the numbers. In the example, the expected size is 12 digits (including the digits after the decimal point).