Thursday, January 03, 2008

Find the CultureInfo by ISO Currency Code


Below is the code that find the CultureInfo by ISO Currency Code

protected CultureInfo CultureInfoFromCurrencyISO( string isoCode )
{
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

foreach (CultureInfo ci in cultures)
{
RegionInfo ri = new RegionInfo(ci.LCID);
if (ri.ISOCurrencySymbol == isoCode)
{
return ci;
}
}
return null;
}

// Since one ISO Currency Code may be associated with more than one LCIDs
// We could rewrite the code to the following

public static IList CultureInfoFromCurrencyISO(string isoCode)

{

    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);

    IList Result = new ArrayList();

    foreach (CultureInfo ci in cultures)

    {

        RegionInfo ri = new RegionInfo(ci.LCID);

        if (ri.ISOCurrencySymbol == isoCode)

        {

            if(!Result.Contains(ci))

                Result.Add(ci);

        }

    }

    return Result;

}

2 comments:

John Hardin said...

One of the LCID values for a given currency code. "USD" (U.S. Dollars) has, for example, six.

Eddie Chen said...

Yes. You are correct about this. As you can tell from the code, my function returns the very first LCID it encounters.
Actually there are 7 LCIDs associated with "USD"
To deal with it, we could re-write our code so it returns an array of CultureInfo that is related to the ISO code.