While converting value of a textbox to Convert.ToSingle()
adds some extra precision for values like 2.7 to 2.7999999523162842. I don't want any rounding of this value because I have to store the exact value entered by the user to DB. So if any one have this issue before please post the solution, any help will appreciated.
Asked
Active
Viewed 191 times
0

p.s.w.g
- 146,324
- 30
- 291
- 331

tharadas das
- 21
- 1
- 2
1 Answers
3
That's because a float
is a binary fraction which cannot exactly represent the number 2.7 -- when you try to assign a number that is not a binary fraction to a float
or a double
, you will inevitably end up with an inexact representation.
You need use a decimal
if you don't want to precise represent decimal numbers like 2.7.
var myDecimal = Convert.ToDecimal(input);
SImilarly, to store this value in a sql server database you need to use a SQL data type capable of storing precise decimal numerics. Conventiently, this data type is called decimal
in SQL as well

p.s.w.g
- 146,324
- 30
- 291
- 331