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.

1

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

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)
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.
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
Thanks, I've changed the column to Decimal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.