-1

I can't get the decimal.TryParse method to work for currency with the euro symbol. I've tried placing the symbol at the end and after a space to no avail. I've also tried different cultures ("fr-FR", "de-DE") and number styles ("Any" combines them all except hexadecimal). The method fails despite the culture showing a matching currency symbol. Parse value with Currency symbol SO solution does not work.

using System;
using System.Globalization;
                    
public class Program
{
    public static void Main()
    {
        var value = "€5,432,109.876";
        var cultureInfo = CultureInfo.CreateSpecificCulture("de-DE");

        decimal result;

        if (!decimal.TryParse(value, NumberStyles.Any, cultureInfo, out result))
        {
            Console.WriteLine(string.Format("Unable to parse '{0}' as a number using the '{1}' culture with currency symbol '{2}'.", value, cultureInfo.IetfLanguageTag, cultureInfo.NumberFormat.CurrencySymbol));
            
            return;
        }

        Console.WriteLine(result.ToString(cultureInfo));
    }   
}

Here's a fiddle with the above.

UPDATE

IMHO the suggested .NET: Parsing localized currency and the other one I looked into don't expand on how the root of the problem is that the culture must match all aspects of the numeric format--thanks to @Zero0 and @Dai for expanding as much on the comments and accepted answer.

Not that it wasn't ignorance of other currency formats on my part, but to see the question down-voted hints at little tolerance for error in this space. I'll keep the question posted given I found value in the answer and comments--others might as well.

hector-j-rivas
  • 771
  • 8
  • 21
  • 1
    `de-DE` and `fr-FR` won't work because they use dots instead of commas for digit-grouping. – Dai Sep 11 '20 at 02:45
  • You would need to use a culture that matches the format of the string you're trying to parse. Your formatted string doesn't match the `de-DE` culture you're trying to use. Just use the correct culture for the string. See duplicate. – Peter Duniho Sep 11 '20 at 02:48
  • See also https://stackoverflow.com/questions/11889694/method-to-parse-decimals-with-various-decimal-radix-separators and https://stackoverflow.com/questions/9160059/set-up-dot-instead-of-comma-in-numeric-values for additional inspiration. – Peter Duniho Sep 11 '20 at 02:50
  • If your Input is forced on you you may be looking for this detail: cultureInfo.NumberFormat.CurrencyPositivePattern = 0; // begining no space cultureInfo.NumberFormat.CurrencyDecimalSeparator = "."; // force decimal as period cultureInfo.NumberFormat.CurrencyGroupSeparator = ","; // force thousands as comma – Sql Surfer Sep 11 '20 at 03:36

1 Answers1

0

Change

var cultureInfo = CultureInfo.CreateSpecificCulture("de-DE");

to

var cultureInfo = CultureInfo.CreateSpecificCulture("en-IE");

Output:

5432109.876

Edit:

As mentioned in the comments this works because of the comma digit grouping of the culture. This answer happens to match (so it works), but I agree it's also important to note why this culture works where others don't.

Zer0
  • 7,191
  • 1
  • 20
  • 34
  • 3
    While this *works* (because Ireland uses English-style commas for digit-grouping), do we know the number is actually from Ireland and not some other culture? I feel a better solution would be to use a specially configured `NumberFormatInfo` object for the `IFormatInfo` parameter rather than an entire `CultureInfo`. – Dai Sep 11 '20 at 02:47
  • I see your point. The short answer works—thanks! The long answer is kinda “you must match *all* aspects of the number to the specific culture”—also thanks! – hector-j-rivas Sep 11 '20 at 02:50