What's the proper way to convert from a scientific notation string such as "1.234567E-06" to a floating point variable using C#?
+14
A:
Double.Parse("1.234567E-06", System.Globalization.NumberStyles.Float);
Will
2008-09-15 16:55:10
Right on Will. Thx.
odbasta
2008-09-15 17:11:00
+4
A:
Also consider using
Double.TryParse("1.234567E-06", System.Globalization.NumberStyles.Float, out MyFloat);
This will ensure that MyFloat
is set to value 0 if, for whatever reason, the conversion could not be performed. Or you could wrap the Double.Parse()
example in a Try..Catch
block and set MyFloat
to a value of your choosing when an exception is detected.
Jaymie
2008-10-21 08:49:33
You don't want to rely on MyFloat being 0 to indicate a failed conversion, you want to rely on the bool return value.
Carl
2008-10-21 08:55:02