Solved

Inserting into Float field in sql table

Posted on 2011-02-17
3
1,505 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
Comment Utility
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
Comment Utility
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
Comment Utility
If you really want the details then look here for more info.
http://en.wikipedia.org/wiki/Floating_point
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL Restore Script - Syntax Error 8 69
Record extraction 3 12
SQL server 2008 SP4 29 31
Sql query for filter 12 20
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now