0

Can someone explain why in SQL Server / T-SQL, this query

SELECT CAST(0.26 AS REAL)

returns

0,25999999046325684

and this query

SELECT CAST(0.27 AS REAL)

returns

0,27000001072883606

but this one

SELECT CAST(0.25 AS REAL)

returns 0,25

When I use datatype MONEY instead of REAL or FLOAT, everything is fine, but I want to learn why this happens...

Ronan Vico
  • 585
  • 3
  • 9
  • 2
    You need to learn about fixed versus floating point number representations. I would suggest starting with Wikipedia or some other source. – Gordon Linoff Jul 18 '19 at 21:07
  • Dude i tried to google but i do not found any of nice sources to study why this happens to me , but thx – Ronan Vico Jul 18 '19 at 21:09
  • it's still weird that doing just that `CAST` it gives a different result (with no other operations going on), I can't replicate that behavior in any database – Lamak Jul 18 '19 at 21:13
  • Try [here](https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/floating-point-numbers) or [here](https://www.red-gate.com/simple-talk/opinion/opinion-pieces/the-practical-problems-of-determining-equality-and-equivalence-in-sql/) found by Googling "t-sql fixed versus floating point number representations" but cannot replicate your first result. – Peter Smith Jul 18 '19 at 21:13

0 Answers0