views:

66

answers:

1

Hi,

I'm trying to convert a string to a double value in .Net 3.5. Quite easy so far with

double.Parse(value);

My problem is that values with exponential tags are not right converted. Example:

double value = double.Parse("8.493151E-2");

The value should be = 0.0893151 right? But it isn't! The value is = 84931.51!!!

How can that be? I'm totally confused!

I read the reference in the msdn library and it confirms that values like "8.493151E-2" are supported. I also tried overloads of double.Parse() with NumberStyles, but no success.

Please help!

+12  A: 

It works for me:

double.Parse("8.493151E-2");  
0.08493151

You're probably running in a locale that uses , for the decimal separator and . for the thousands separator.
Therefore, it's being treated as 8,493,151E-2, which is in fact equivalent to 84,931.51.

Change it to

double value = double.Parse("8.493151E-2", CultureInfo.InvariantCulture);
SLaks
Ok, that works!I didn't try to use it with IFormatProviderThanks!
flashflail
@flashfail - if you don't specify an `IFormatProvider` it will use your computer's locale. You give your location as "DE Oldenburg", which I'm assuming is in Germany - where you use a decimal comma and "." as the thousands separator.
ChrisF
@flashflail: Then you should accept this answer by clicking the hollow check.
SLaks