2

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

asked Oct 27, 2010 at 12:26
2
  • I hope you meant that you stored 14.36 in the field... Commented Oct 27, 2010 at 12:39
  • please see my edit and the providet links Commented Oct 27, 2010 at 13:10

2 Answers 2

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:

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)

answered Oct 27, 2010 at 12:33
Sign up to request clarification or add additional context in comments.

1 Comment

so, how can i store the float value in table? what type i must use? (it's boring to have two fields for each value:/)
1

Or you better use "decimal" with length 10,2 or something like that for storing prices.

answered Oct 27, 2010 at 13:08

2 Comments

double is still a floating point value. Doing a display length of 10,2 won't change the rounding issues with floats. At most it'll just hide them and lead to greater confusion while debugging.
My mistake. I meant decimal(10,2) as @oezi wrote.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.