C# FormatException double.parse(), Why isn't 0.89 parsing?

244 Views Asked by At
class Program
{
    static void Main(string[] args)
    {
        string str = "0.898";
        double dbl = Double.Parse(str);

        dbl++;

        Console.WriteLine(dbl);
        Console.ReadLine();
    }
}

All other formats that I need to work, like "100" works. But as soon as I add a 'dot' I have a FormatException error.

1

There are 1 best solutions below

1
On BEST ANSWER

maybe try:

double dbl = double.Parse(str , CultureInfo.InvariantCulture);

check here at ideone

Your problem is that your culture does not allow dot's. Invariant culture is not the only solution, you can also specify your culture and use it's separators. If your current culture accepts only commas as separators it could also be a solution to replace dot with comma. Not specifying culture explicitly will effect in problems with parsing numbers in different machines running different cultures.

Everything that is culture specific is always tricky and should be defined as precise as it's possible. If you know excatly which double format you will use, define it. If you know which date format you will use, specify it etc.