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

Simon
  • 22,637
  • 36
  • 92
  • 121

2 Answers2

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)

Community
  • 1
  • 1
oezi
  • 51,017
  • 10
  • 98
  • 115
  • 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:/) – Simon Oct 27 '10 at 12:38
1

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

Yasen Zhelev
  • 4,045
  • 3
  • 31
  • 56
  • 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 Oct 27 '10 at 17:26
  • My mistake. I meant decimal(10,2) as @oezi wrote. – Yasen Zhelev Oct 28 '10 at 08:34