• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1942
  • Last Modified:

Inserting into Float field in sql table

hi! i am reading a line from a csv file, splitting the line using the split method, and parsing the array, inserting the values into an sql table. one of my fields is defined as type float. i using the following code to convert the string to a float and insert into my field. the float inserted is correct except that it has a lot of garbage added to it. does anyone know why this happens?

here is the code:
value = splitLine[lineIndex];
            insertSql.AddFieldValuePair("VIN", "@VINParam");
            Single f = Convert.ToSingle(value);
            if (myCommand.Parameters.IndexOf("@VINParam") < 0)
                myCommand.Parameters.Add("@VINParam", System.Data.SqlDbType.Float).Value = f;
            else
                myCommand.Parameters["@VINParam"].Value = value;
            lineIndex += 1;

for example, when the float value is: 16.43437

i get:16.434370040893555
0
EylonM
Asked:
EylonM
  • 2
1 Solution
 
TheAnvilGroupCommented:
Floats are approximate-number data types for use with floating point numeric data. Floating point data is approximate; therefore, not all values in the data type range can be represented exactly.

Use the decimal data type to store numbers with decimals when the data values must be stored exactly as specified.

0
 
AndyAinscowCommented:
That isn't garbage, that is how the computer has that number stored in memory.  Non integral values are just an 'approximate' value
0
 
AndyAinscowCommented:
If you really want the details then look here for more info.
http://en.wikipedia.org/wiki/Floating_point
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now