Look at this query please
SELECT max( val_amd ) FROM `best_deposits`
I have the max value in the table equal to 14.6(the fields has type float),
But it returns 14.3599996566772
why does it happen, and how can i get the exact value?
Thanks much
-
I hope you meant that you stored 14.36 in the field...Ned Batchelder– Ned Batchelder2010年10月27日 12:39:12 +00:00Commented Oct 27, 2010 at 12:39
-
please see my edit and the providet linksoezi– oezi2010年10月27日 13:10:57 +00:00Commented Oct 27, 2010 at 13:10
2 Answers 2
floats are evil!
NEVER use floats for storing amounts or prices. instead of that, use an int and store the amount in cents. thats the only way to get around those problems forever.
why this happens: because floats can't be saved exactly in many cases (such as 0.6 in your case)
PS: we had those questions a hundret times for different languages till now:
- Use Float or Decimal for Accounting Application Dollar Amount?
- PHP rounding problem (5.2.3)?
- Rounding problem with double type
- Javascript rounding v c# rounding
- Python rounding problem
- ... and a lot more
EDIT: to your comment: as i said:
use an int and store the amount in cents
(alternatively you could use an DECIMAL(10,2) (or how big/how much decimal places you need)... not sure about how this works)
1 Comment
Or you better use "decimal" with length 10,2 or something like that for storing prices.