convert to float

Question

•

how do i correctly convert string to float...

for example, the below code:

using System;

class FloatTest {

public static void Main() {

String s1 = "99.99999999";
Console.WriteLine("" + Convert.ToSingle(s1));

}
}

the above prints 100

the string 99.99999999 gets converted to 100...

I can understand this is something to do with floating point arithmetic... but i forgot how to resolve this...

So if we have a float value stored as a string, how to convert this string float to float successfully?

what is the correct way to do this?

i wil appreciate help on this...

Thursday, August 30, 2007 1:20 PM

• Single values have only 6 significant digits, it cannot accurately represent your value.  Try this code for example:

float f = 33.33333333f;
Console.WriteLine("{0:N15}, {1:N15}", f, 3 * f);

Now change "float" to "double" and drop the "f" postfix.  "decimal" is usually your best bet.
Thursday, August 30, 2007 2:31 PM
• You are correct that it is a rounding issue.  The problem is that 99.99999999 isn't a valid float value.  Try it for yourself.

float f = 99.99999999F;

Floats have a precision of 7 digits externally (9 internally) so if you go above that then rounding is automatic.  So if you drop the float down to 7 digits (2 to the left, 5 to the right) then it'll work out and the string conversion will as well.

There really isn't any workaround.  If you try to pass a number that exceeds the precision then it is going to get rounded.  So if you were to change the value to 99.999996 or higher then it would round up.  If you use 99.999995 or less then it'll round down.  The difference of 1 is probably caused by the internal layout of the IEEE number.

As an aside the alternative mechanism is to use Float.Parse.  In this particular case you can go up to 99.999996 and it'll round down.  I doubt this is anything more than a slight difference in the code.  You'd probably get different results with different values.

Michael Taylor - 8/30/07

http://p3net.mvps.org

Thursday, August 30, 2007 2:33 PM

All replies

• Single values have only 6 significant digits, it cannot accurately represent your value.  Try this code for example:

float f = 33.33333333f;
Console.WriteLine("{0:N15}, {1:N15}", f, 3 * f);

Now change "float" to "double" and drop the "f" postfix.  "decimal" is usually your best bet.
Thursday, August 30, 2007 2:31 PM
• You are correct that it is a rounding issue.  The problem is that 99.99999999 isn't a valid float value.  Try it for yourself.

float f = 99.99999999F;

Floats have a precision of 7 digits externally (9 internally) so if you go above that then rounding is automatic.  So if you drop the float down to 7 digits (2 to the left, 5 to the right) then it'll work out and the string conversion will as well.

There really isn't any workaround.  If you try to pass a number that exceeds the precision then it is going to get rounded.  So if you were to change the value to 99.999996 or higher then it would round up.  If you use 99.999995 or less then it'll round down.  The difference of 1 is probably caused by the internal layout of the IEEE number.

As an aside the alternative mechanism is to use Float.Parse.  In this particular case you can go up to 99.999996 and it'll round down.  I doubt this is anything more than a slight difference in the code.  You'd probably get different results with different values.

Michael Taylor - 8/30/07

http://p3net.mvps.org

Thursday, August 30, 2007 2:33 PM
• thanks nogugz and taylor... your replies did really help...

Friday, August 31, 2007 11:31 AM