.Net: Getting the Currency Symbol from a Currency Code

by matt 13. March 2009 12:02

Many year ago I built an e-commerce solution where I was receiving the international currency code (I.e. GBP or USD) and had to use this to determine the currency symbol to display.  This was during the dirty days of ASP.Classic so I just slapped in a 'nice' little switch statement and that was that.

That is neither elegant or good, and now faced with a similar issue I decided to look in to the problem and get a nicer solution.

The Problem

I'm receiving ISO 4217 currency codes in my application and that application in itself is running under a current culture of en-US. 

Using this code, I need to determine the region or culture based information in order to display currencies to the end user.

The Solution

After a bit of looking about on the MSDN site at RegionInfo and CultureInfo I came up with the following LINQ query to return the RegionInfo:

string currencyCode = "PLN";
 
System.Globalization.RegionInfo regionInfo = (
    from culture in System.Globalization.CultureInfo.GetCultures(
        System.Globalization.CultureTypes.InstalledWin32Cultures
        )
    let region = new System.Globalization.RegionInfo(culture.LCID)
    where String.Equals(
        region.ISOCurrencySymbol, 
        currencyCode, 
        StringComparison.InvariantCultureIgnoreCase
        )
    select region).First();
 
Console.WriteLine(regionInfo.CurrencySymbol);

Alternatively, we can return the CultureInfo itself and use String.Format to do the work for us.

string currencyCode = "BRL";
decimal value = 19.99M;
 
System.Globalization.CultureInfo cultureInfo = (
    from culture in System.Globalization.CultureInfo.GetCultures(
        System.Globalization.CultureTypes.InstalledWin32Cultures
        )
    let region = new System.Globalization.RegionInfo(culture.LCID)
    where String.Equals(
        region.ISOCurrencySymbol, 
        currencyCode, 
        StringComparison.InvariantCultureIgnoreCase
        )
    select culture).First();
 
Console.WriteLine(
    String.Format(
        cultureInfo,
        "{0:C}",
        value
        )
    );

Job done.

Tags:

.Net Framework | LINQ

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar