Input string was not in a correct format #2
In order to convert string to double without an exception:
An unhandled exception of type System.FormatException occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
make it culture-insensitive by providing second parameter value CultureInfo.InvariantCulture, for example:
double.Parse("1234.5678", CultureInfo.InvariantCulture)
first solution (as mentioned in other posts):
double temp = double.Parse("1234.5678", CultureInfo.InvariantCulture);
second solution - make it default to current thread:
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
double temp = double.Parse("1234.5678");
third solution - make it default to block of code:
var prevCurrentCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
...
double temp = double.Parse("1234.5678");
...
Thread.CurrentThread.CurrentCulture = prevCurrentCulture;
As far as I know the Convert
methods use the current locale to do such conversions. In this case I'd guess your current locale would expect a comma as decimal point. Try to set the current locale for your application or the conversion to some language/country where dots are used (e.g. en_US). The method should provide a second optional parameter to provide a IFormatProvider as an alternative solution.