C# .NET - Float value problem - Asked By sanjay CP on 19-Feb-12 11:08 PM

hi,,,,,,,,



   i am using float datatype to take some value from textbox and inserting into sql server 2005 database column float datatype, but the problem is if i enter 101.1 it getting stored as 101.09999847412109. i need to be stored as 101.1, i mean as i enter the float value, as it is it has to be stored.please help me.


Entered into Textbox 101.1-----------stored in sql server 2005 as  101.09999847412109---------but i need as it is ,it has to be stored in sql server 2005...i.e. 101.1


Thanking You
Venkat K replied to sanjay CP on 19-Feb-12 11:35 PM
In your SQL Insert query use the round function to round it to two decimal places:

ROUND(TextBox1.Text, 2)

Anoop S replied to sanjay CP on 20-Feb-12 01:58 AM
You need to use Decimal fields if you want to save 101.1 to 101.1

float and double are floating binary point types. In other words, they represent a number like this: 101.09999999875
The binary number and the location of the binary point are both encoded within the value.
decimal is a floating decimal point type. In other words, they represent a number like this: 123.45
Again, the number and the location of the decimal point are both encoded within the value - that's what makes decimal still a floating point type instead of a fixed point type.

Refer this if you want more details regarding Decimal floating point and Binary floating point

http://www.yoda.arachsys.com/csharp/decimal.html
http://www.yoda.arachsys.com/csharp/floatingpoint.html
Anoop S replied to Anoop S on 20-Feb-12 02:00 AM
In sql its Single instead of Decimal