Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
why if you do
SQL
declare @p float='29545428.022495'

select cast(@p as numeric(17,9))

you obtain this?
29545428.022495002

please can you explain the differece of this type and help me to understand why the value change?
thank you
Posted
Comments
PIEBALDconsult 25-Aug-15 17:07pm    
It doesn't. Avoid floats.

1 solution

You'll always get slight differences converting between float\numeric types in sql (double\decimal in c#) because the float\double type doesn't hold the exact number you want, it holds a mathematical representation that is close to what you want. This allows it to store far greater\smaller numbers than their "actual number" equivalents like numeric\decimal. If numeric precision is crucial to you then floats are not a good choice.
 
Share this answer
 
Comments
gregorio89 26-Aug-15 3:22am    
but why exists a data type that can't store the perfect assigned value ?
F-ES Sitecore 26-Aug-15 3:52am    
Numbers take up space, and a system can only do maths etc on numbers up to a certain size. If you want really really big, or really really small numbers then you are going beyond the standard number size limits so the language allows the use of fuzzy variables that can hold bigger values as the values themselves are not held literally, but a mathematical approximation of them is.

It's your job as a developer to fully understand the underlying data types and language features and use what is appropriate.
gregorio89 26-Aug-15 5:03am    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900