Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get CultureInfo from a Currency Code?

Tags:

.net

asp.net

I need to get the System.Globalization.CultureInfo for different Currency Codes.

Examples: EUR, GBP, USD

Currently I am doing the following inside a switch statement based on this 3 letter currency code, obviously this is not the way to do it:

var ci = new System.Globalization.CultureInfo("en-US"); 

Anyone have a clean way to achieve the same results using the currency code's instead?

like image 865
Slee Avatar asked Feb 10 '12 16:02

Slee


2 Answers

static IEnumerable<CultureInfo> GetCultureInfosByCurrencySymbol(string currencySymbol)
{
    if (currencySymbol == null)
    {
        throw new ArgumentNullException("currencySymbol");
    }

    return CultureInfo.GetCultures(CultureTypes.SpecificCultures)
        .Where(x => new RegionInfo(x.LCID).ISOCurrencySymbol == currencySymbol);
}

For example

foreach (var culture in GetCultureInfosByCurrencySymbol("GBP"))
{
    Console.WriteLine(culture.Name);
}

prints:

cy-GB
gd-GB
en-GB
like image 79
Paolo Moretti Avatar answered Oct 14 '22 08:10

Paolo Moretti


Short: That's not possible. EUR for example would map to de-DE, fr-FR, nl-NL and other countries. There is no mapping from Currency to culture, because multiple countries share currencies.

like image 24
Sebastian P.R. Gingter Avatar answered Oct 14 '22 07:10

Sebastian P.R. Gingter