Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong value with double.Parse(string)

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!

like image 801
Kai Avatar asked Dec 07 '22 02:12

Kai


1 Answers

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);
like image 108
SLaks Avatar answered Jan 22 '23 11:01

SLaks