I have a float column in SQL Server with value 21.261 , when I am fetching this column into c# double , using entity framework core 2.0 it is becoming 21.2610000000042, how to avoid this and get the exact value as 21.261
Advertisement
Answer
As you may have noticed , your number is probably not exactly 21.261, but only some pretty accurate approximation.
Depending on what your data really represents you could use a different database storage type like ‘DECIMAL’ … that would typically help when storing monetary values, that have ‘cents’.
However, depending on the programming language being capable of working with rational numbers or fractions, the moment you would read in the data back into a float, you again have to deal with these issues and likewise have to deal with it during printing etc.