views:

27

answers:

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

+2  A: 

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)

oezi
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:/)
Syom
please see my edit and the provided links
oezi
+1  A: 

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

Yasen Zhelev
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.
Marc B
My mistake. I meant decimal(10,2) as @oezi wrote.
Yasen Zhelev