Solved

Inserting into Float field in sql table

Posted on 2011-02-17
3
1,578 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 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