Solved

Inserting into Float field in sql table

Posted on 2011-02-17
3
1,636 Views
Last Modified: 2012-05-11
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
Comment
Question by:EylonM
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
TheAnvilGroup earned 125 total points
ID: 34914586
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34914598
That isn't garbage, that is how the computer has that number stored in memory.  Non integral values are just an 'approximate' value
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34914621
If you really want the details then look here for more info.
http://en.wikipedia.org/wiki/Floating_point
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question