.NET Decimal.Parse throws a FormatException when using period as thousand separator

In .NET, can anyone tell me why this works:

Decimal.Parse("1,234.56", CultureInfo.GetCultureInfo("en-US"));

But this throws a System.FormatException?

Decimal.Parse("1.234,56", CultureInfo.GetCultureInfo("fr-FR"));

Is there a recommended approach to parsing European numbers which might contain a period as the thousand separator and a comma as the decimal point?

Thank you!

Jon Skeet
people
quotationmark

The French CultureInfo's NumberFormatInfo uses space as the NumberGroupSeparator - so this works, for example:

Decimal.Parse("1 234,56", CultureInfo.GetCultureInfo("fr-FR"));

If you want to use . as a thousands separator, you'll need a culture where that is the NumberGroupSeparator. For example, you could use Spanish:

using System;
using System.Globalization;

class Test
{
    static void Main()        
    {
        var culture = (CultureInfo) CultureInfo.GetCultureInfo("es-ES");
        var d = Decimal.Parse("1.234,56", culture);
        Console.WriteLine(d);
    }
}

Or modify another culture:

using System;
using System.Globalization;

class Test
{
    static void Main()        
    {
        // Call clone to make sure it's mutable
        var culture = (CultureInfo) CultureInfo.GetCultureInfo("fr-FR").Clone();
        culture.NumberFormat.NumberGroupSeparator = ".";
        var d = Decimal.Parse("1.234,56", culture);
        Console.WriteLine(d);
    }
}

people

See more on this question at Stackoverflow