henfo <he***@discussions.microsoft.com> wrote:
This should be simple, but either I'm blind, stupid or going mad.
I'm trying to convert a string, containing a numeric value to a double (in
C#), but when I run the code below I get the error message "Input string was
not in a correct format." Why?
string s = "8.0";
double d = Convert.ToDouble(s);
I suspect the problem is the locale you're running in. Try this:
using System;
using System.Threading;
using System.Globalization;
public class Test
{
static void Main()
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture("en");
Console.WriteLine (Convert.ToDouble("8.0"));
}
}
Now change the "en" to "fr" and run it again, and you'll see the error.
If you want to do the conversion in the invariant culture, do:
Convert.ToDouble(s, CultureInfo.InvariantCulture);
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too