Invalid value with double.Parse (string)

I am trying to convert a string to a double value in .Net 3.5. Pretty easy so far with

double.Parse(value); 

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

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

Value must be = 0.0893151 right? But this is not so! Value = 84931.51 !!!

How can it be? I am completely confused!

I read the link in the msdn library and confirmed that values ​​like "8.493151E-2" are supported. I also tried to overload double.Parse () with NumberStyles, but did not succeed.

Please, help!

+4
source share
1 answer

This works for me:

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

You are probably working on the locale that uses the decimal separator and . for thousands separator.
Therefore, it is considered as 8,493,151E-2 , which is actually equivalent to 84,931.51 .

Change it to

 double value = double.Parse("8.493151E-2", CultureInfo.InvariantCulture); 
+15
source

Source: https://habr.com/ru/post/1315531/


All Articles