0

I've seen some errors in my life, but this one caught me by surprise the most.

I have a float column containing 0.02.

Now i'm trying this query :

select rma.*,120.0/60.0*rma.amount as reward

Now math tells us the result will be 0.04, But suprisingly enough MySql (Version 5.5.28) claims otherwise, and says it's :

0.03999999910593033

I've also tried without the .0 with the numbers, and using braces, still same result.

Now the same results happens with all cases of uneven numbers. for example 4.1 will be 8.199999809265137

Anyone knows why that happens?

eric.itzhak
  • 15,752
  • 26
  • 89
  • 142

1 Answers1

0

Try to CAST it like so:

select rma.*, CAST((120.0 / 60.0 * rma.amount) AS DECIMAL(10,2)) as reward
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
  • Well that did solve it. Any idea why that happend? why do you have to cast it? can you provide a short explination please. – eric.itzhak Jan 10 '13 at 15:26
  • @eric.itzhak - I am not sure how MySQL treats a new created column from a multiplied columns, it seems that it doesn't perceive a specific precision for it. So `DECIMAL(10, 2)` will give you in two digit precision. – Mahmoud Gamal Jan 10 '13 at 15:31