James Hollingworth’s Adventures in Code

Icon

Get the currency symbol for the ISO 4217 Currency Code (C#)

Had a bit of a problem today. After importing a bunch of transactions I needed to display the corresponding currency symbol. The problem was when you import OFX transactions, the currency is in the ISO 4217 three letter format. Basically the little bit of LINQ below will search through all the culture infos until it finds one with the correct ISO Currency symbol. Its little things like this that make me love linq!

RegionInfo regionInfo = (from c in CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures)
let r = new RegionInfo(c.LCID)
where r.ISOCurrencySymbol == "GBP"
select r).First();

Hope this is helpful to someone!

Filed under: .net, c#, linq, ofx ,

5 Responses

  1. Jon says:

    Thanks that helped! Was unaware of the RegionInfo.ISOCurrencySymbol.

  2. postbackmouton says:

    Helped me a lot. And I was actually only wondering if I could get all the cultures and sort them through a query.

    Don’t you love LINQ?

  3. Andrew Hancox says:

    You can actually make it simpler than this, since the iso currency code is the two letter country code followed by a single digit currency id the following will work:

    string currencysymbol=
    new RegionInfo(CurrencyCode.Substring(0, 2)).CurrencySymbol;

  4. Andrew Hancox says:

    Stop!!! You can’t do it the way I just suggested due to pan-national currencies. Hope no one followed my advice!

  5. codefornothing says:

    Not sure if this is Win 7 specific or something but the LINQ above fails for me on the InvariantCulture. To Solve the problem I used CultureTypes.SpecificCultures instead of CultureTypes.InstalledWin32Cultures.

    I hope this helps

Leave a Reply