?
Solved

Inserting into Float field in sql table

Posted on 2011-02-17
3
Medium Priority
?
1,741 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
TheAnvilGroup earned 500 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

743 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