4

When I am Entering a double number from C# to the DB I use a real type in the DB. When I check the value it is exactly what I enter.

Now when I go to retreive the value from the DB and store it into a C# double, it adds random decimal values at then end. The number in the database is the correct value that I want, but the value in C# as a double is just random (sometimes higher sometimes lower then the actual value.)

ie

- Enter into the db 123.43 as a double to an sql real.
- View the value in the DB, it's exactly 123.43
- Get the value from the DB and store into a C# double, value in the double is now 123.4300000305176

I have tried changing the type to float, decimal, etc. in the DB but these types actually alter the value when I put it into the DB to the same format as the above.

Any help on whats going on or how to fix it?

DMCApps
  • 2,110
  • 4
  • 20
  • 36

2 Answers2

3

You should probably be using Decimal type. See What represents a double in sql server? for further explanation.

Community
  • 1
  • 1
Allov
  • 1,308
  • 1
  • 13
  • 23
  • yes I have attempted to use Decimal(13,8) but this makes the value do the same thing as the above. Except in this case it occurs on putting the value into the DB as opposed to only when I attempt to retrieve it. – DMCApps Jul 14 '11 at 17:28
  • 3
    You need to use Decimal in both the database and your C# code. – Dour High Arch Jul 14 '11 at 17:37
  • So I made both decimal in the DB and decimal in C# and it works ... but can anyone explain why real to double would have this behavior? Especially when it occurs in one table but not another :S – DMCApps Jul 14 '11 at 19:01
  • 1
    Float and Doubles are basicaly approximations of the actual number since it would require too much calculation. You can have a look at this http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c for a more complete explanation. – Allov Jul 14 '11 at 19:30
0

Try using a Single if your DB type is real. Check here for data mappings http://msdn.microsoft.com/en-us/library/ms131092.aspx

Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55