Converting a string to a double
-
23-08-2019 - |
Question
I'm trying to convert a string to a double value but it's not returning me what I expect...
double dbl;
Double.TryParse("20.0", out dbl);
That piece of code is returning 200.0 (instead of 20.0) as a double value. Any idea why?
Solution
You should pass InvariantCulture
to the method.
The reason behind this is that your regional settings probably set .
as separator character and not decimal point.
double.TryParse("20.0", NumberStyles.Any,
CultureInfo.InvariantCulture, out x);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow