I am parsing an xml file and I am trying to assign a value which I is read from xml file into a double variable.
here is what my code looks like:
double someDouble = Convert.ToDouble(someString);
problem here is, when executing this line of code, i get
An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
When I debug, i see that the value of someString is "45.00000000" in the "Locals" view.
As far as I know Convert.ToDouble supports such converting strings into double.
NOTE: just to be sure I also tried
double someDouble = Convert.ToDouble(someString.Trim('"'));
It works fine for me.
My psychic debugging skills tell me that you're running in a culture that uses , as the decimal separator.
Pass CultureInfo.InvariantCulture.
The code you wrote should work so long as the string being converted is actually a number. Maybe there is a non-number somewhere in the string that you are not seeing. That is the only thing that I can think of.
This won't solve your particular problem but I would recommend Double.TryParse instead of Convert.ToDouble. You largely eliminate the chance that an exception will be thrown with Double.TryParse
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With