1

Why when I float.Parse a string "109.7" and insert the result into the DB to a 'float' column I get 109.69999694824219 instead of 109.7?

I am using Microsoft SQL Server 2008 and C#. The string of the number is taken from a Text input field.

mskfisher
  • 3,177
  • 3
  • 33
  • 47
Liron Harel
  • 10,321
  • 25
  • 111
  • 211

1 Answers1

4

floats do not have full precision. You should use decimal.Parse instead.

Leslie Hanks
  • 2,287
  • 3
  • 28
  • 41
  • But my DB column is set to float data type. Do I need to change it to decimal instead of float - I mean for the DB column? or like this: float width = (float)decimal.Parse(w) – Liron Harel Aug 13 '11 at 21:04
  • Well then you will need to round the number when you pull it out of the database or change the type in the database to a decimal. – Leslie Hanks Aug 13 '11 at 21:06
  • 2
    If you need precision, then your column has the wrong data type. ALTER TABLE dbo.MyTable ALTER COLUMN MyColumn decimal(18,2) With a scale and precision that makes sense for your application – billinkc Aug 13 '11 at 21:08
  • Thanks, I've changed the column to Decimal – Liron Harel Aug 13 '11 at 21:19