61

In C# is it possible to get a currency symbol, like '£', from the 3 character currency code, in this case 'GBP'?

Is this possible either in SQL Server or in C#?

Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304

9 Answers9

103

While a bit brute-force and not particularly elegant, you could do it like this:

public bool TryGetCurrencySymbol(string ISOCurrencySymbol, out string symbol)
{
    symbol = CultureInfo
        .GetCultures(CultureTypes.AllCultures)
        .Where(c => !c.IsNeutralCulture)
        .Select(culture => {
            try{
                return new RegionInfo(culture.Name);
            }
            catch
            {
                return null;
            }
        })
        .Where(ri => ri!=null && ri.ISOCurrencySymbol == ISOCurrencySymbol)
        .Select(ri => ri.CurrencySymbol)
        .FirstOrDefault();
    return symbol != null;
}

and use it as follows:

string currSymbol;
if(TryGetCurrencySymbol("GBP",out currSymbol))
{
    Console.WriteLine("symbol is {0}", currSymbol);
}

If you anticipate hammering this method, perhaps it's better to build a cache up front:

public static class CurrencyTools
{
    private static IDictionary<string,string> map;
    static CurrencyTools()
    {
        map = CultureInfo
            .GetCultures(CultureTypes.AllCultures)
            .Where(c => !c.IsNeutralCulture)
            .Select(culture => {
                try{
                    return new RegionInfo(culture.Name);
                }
                catch
                {
                    return null;
                }
            })
            .Where(ri => ri!=null)
            .GroupBy(ri => ri.ISOCurrencySymbol)
            .ToDictionary(x => x.Key, x => x.First().CurrencySymbol);
    }
    public static bool TryGetCurrencySymbol(
                          string ISOCurrencySymbol, 
                          out string symbol)
    {
        return map.TryGetValue(ISOCurrencySymbol,out symbol);
    }
}

At the time of writing, on my machine etc. etc. the map contains the following mappings:

AED  د.إ.‏
AFN  ؋
ALL  Lekë
AMD  ֏
ANG  NAf.
AOA  Kz
ARS  $
AUD  $
AWG  Afl.
AZN  ₼
BAM  КМ
BBD  $
BDT  ৳
BGN  лв.
BHD  د.ب.‏
BIF  FBu
BMD  $
BND  $
BOB  Bs
BRL  R$
BSD  $
BTN  Nu.
BWP  P
BYN  Br
BZD  $
CAD  $
CDF  FC
CHF  CHF
CLP  $
CNY  ¥
COP  $
CRC  ₡
CUP  $
CVE  ​
CZK  Kč
DJF  Fdj
DKK  kr.
DOP  $
DZD  د.ج.‏
EGP  ج.م.‏
ERN  Nfk
ETB  Br
EUR  €
FJD  $
FKP  £
GBP  £
GEL  ₾
GHS  GH₵
GIP  £
GMD  D
GNF  FG
GTQ  Q
GYD  $
HKD  $
HNL  L
HRK  kn
HTG  G
HUF  Ft
IDR  Rp
ILS  ₪
INR  ₹
IQD  د.ع.‏
IRR  ريال
ISK  kr
JMD  $
JOD  د.ا.‏
JPY  ¥
KES  Ksh
KGS  сом
KHR  ៛
KMF  CF
KPW  ₩
KRW  ₩
KWD  د.ك.‏
KYD  $
KZT  ₸
LAK  ₭
LBP  ل.ل.‏
LKR  රු.
LRD  $
LYD  د.ل.‏
MAD  د.م.‏
MDL  L
MGA  Ar
MKD  ден
MMK  K
MNT  ₮
MOP  MOP$
MRU  MRU
MUR  Rs
MVR  ރ.
MWK  MK
MXN  $
MYR  RM
MZN  MTn
NAD  $
NGN  ₦
NIO  C$
NOK  kr
NPR  रु
NZD  $
OMR  ر.ع.‏
PAB  B/.
PEN  S/
PGK  K
PHP  ₱
PKR  Rs
PLN  zł
PYG  ₲
QAR  ر.ق.‏
RON  lei
RSD  дин.
RUB  ₽
RWF  RF
SAR  ر.س.‏
SBD  $
SCR  SR
SDG  ج.س.
SEK  kr
SGD  $
SHP  £
SLL  Le
SOS  S
SRD  $
SSP  £
STN  Db
SYP  ل.س.‏
SZL  E
THB  ฿
TJS  смн
TMT  m.
TND  د.ت.‏
TOP  T$
TRY  ₺
TTD  $
TWD  NT$
TZS  TSh
UAH  ₴
UGX  USh
USD  $
UYU  $
UZS  сўм
VES  Bs.S
VND  ₫
VUV  VT
WST  WS$
XAF  FCFA
XCD  EC$
XDR  XDR
XOF  CFA
XPF  FCFP
YER  ر.ي.‏
ZAR  R
ZMW  K
spender
  • 117,338
  • 33
  • 229
  • 351
  • @spender Brilliant reply!! – user1799214 Jun 12 '13 at 09:54
  • 1
    @spender, I have used your approach and removed try/catch block using filtering only specific culture types CultureInfo cultureInfo = CultureInfo.GetCultures(CultureTypes.InstalledWin32Cultures|CultureTypes.SpecificCultures) .FirstOrDefault(c => !c.IsNeutralCulture && !c.Equals(CultureInfo.InvariantCulture) && new RegionInfo(c.LCID).ISOCurrencySymbol == _currency.IsoLetterCode) ??CultureInfo.CurrentCulture; – Rudolf Dvoracek Aug 04 '15 at 07:33
  • 1
    .Select can be simplified with no exception thrown: .Select(culture => string.IsNullOrWhiteSpace(culture.Name) ? null : new RegionInfo(culture.Name)) – miciry89 Aug 10 '19 at 11:23
51

.NET has CultureInfo.NumberFormat.CurrencySymbol

CultureInfo us = new CultureInfo("en-US");
CultureInfo gb = new CultureInfo("en-GB");
CultureInfo fr = new CultureInfo("fr-FR");

Console.Out.WriteLine(us.NumberFormat.CurrencySymbol); // $
Console.Out.WriteLine(gb.NumberFormat.CurrencySymbol); // £
Console.Out.WriteLine(fr.NumberFormat.CurrencySymbol); // €

But this requires the culture name, not "GBP". As far as I know its not possible directly from "GBP", etc.

The same information is also available via RegionInfo, along with the currency code:

RegionInfo us = new RegionInfo("en-US");
RegionInfo gb = new RegionInfo("en-GB");
RegionInfo fr = new RegionInfo("fr-FR");

Console.Out.WriteLine(us.CurrencySymbol); // $
Console.Out.WriteLine(gb.CurrencySymbol); // £
Console.Out.WriteLine(fr.CurrencySymbol); // €

Console.Out.WriteLine(us.ISOCurrencySymbol); // USD
Console.Out.WriteLine(gb.ISOCurrencySymbol); // GBP
Console.Out.WriteLine(fr.ISOCurrencySymbol); // EUR

I suppose one could conceivably use that to construct a map from ISO code to symbol.

The list of culture names is avaliable here.

EDIT: Well this seems to work:

public static class CurrencyCodeMapper
{
    private static readonly Dictionary<string, string> SymbolsByCode;

    public static string GetSymbol(string code) { return SymbolsByCode[code]; }

    static CurrencyCodeMapper()
    {
        SymbolsByCode = new Dictionary<string, string>();

        var regions = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                      .Select(x => new RegionInfo(x.LCID));

        foreach (var region in regions)
            if (!SymbolsByCode.ContainsKey(region.ISOCurrencySymbol))
                SymbolsByCode.Add(region.ISOCurrencySymbol, region.CurrencySymbol);
    }
}

Usage:

CurrencyCodeMapper.GetSymbol("USD") // $
CurrencyCodeMapper.GetSymbol("GBP") // £
CurrencyCodeMapper.GetSymbol("EUR") // €

Note, of course, that this does not produce a comprehensive list. In particular, it does not include old Eurozone currencies that have been superseded by the Euro. I cannot see any way around this but to manually add such currencies if you need them, for example SymbolsByCode.Add("FRF", "₣"); for French Francs.

verdesmarald
  • 11,646
  • 2
  • 44
  • 60
  • This is by far the most elegant answer on this page! The caching upon construction will make it much quicker than all the other solutions which require multiple lookups. – Ian May 02 '14 at 09:44
  • 1
    Best answer (after the Edit section)! Thanks, verdesmarald! – Hajjat Nov 28 '15 at 09:35
  • 2
    I'm getting `ArgumentException: Customized cultures cannot be passed by LCID, only by name.Parameter name: culture`. Easy fix was to replace `.LCID` with `.Name`. Seems to work. – angularsen Jan 07 '21 at 16:15
9

The RegionInfo class has a CurrencySymbol property, so it's doable in C#. You could perhaps use a C# stored procedure if you wanted to do it in Sql Server.

RegionInfo regionInfo = new RegionInfo("GB");
Console.WriteLine(regionInfo.CurrencySymbol); // £

(You need to use the ISO country codes)

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • 1
    The RegionInfo class also has ISOCurrencySymbol which is the three-letter code. So this would be my suggestion as well. The trick is efficiently getting the region with a particular ISO currency code from the available regions. – KeithS Sep 11 '12 at 16:13
8

This will not work on Windows Phone applications as CultureInfo.GetCultures is not available on the platform (at least not yet). So here is a quick and dirty solution - made with the help of spender's answer containing all the culture codes and currencies at the date.

public static class CurrencyHelper
{
    public static string GetCurrencySymbol(string code)
    {
        if (Currencies.ContainsKey(code))
        {
            return Currencies[code];
        }
        else
        {
            return code;
        }
    }

    public static Dictionary<string, string> Currencies = new Dictionary<string, string>() {
                                                    {"AED", "د.إ.‏"},
                                                    {"AFN", "؋ "},
                                                    {"ALL", "Lek"},
                                                    {"AMD", "դր."},
                                                    {"ARS", "$"},
                                                    {"AUD", "$"}, 
                                                    {"AZN", "man."}, 
                                                    {"BAM", "KM"}, 
                                                    {"BDT", "৳"}, 
                                                    {"BGN", "лв."}, 
                                                    {"BHD", "د.ب.‏ "},
                                                    {"BND", "$"}, 
                                                    {"BOB", "$b"}, 
                                                    {"BRL", "R$"}, 
                                                    {"BYR", "р."}, 
                                                    {"BZD", "BZ$"}, 
                                                    {"CAD", "$"}, 
                                                    {"CHF", "fr."}, 
                                                    {"CLP", "$"}, 
                                                    {"CNY", "¥"}, 
                                                    {"COP", "$"}, 
                                                    {"CRC", "₡"}, 
                                                    {"CSD", "Din."}, 
                                                    {"CZK", "Kč"}, 
                                                    {"DKK", "kr."}, 
                                                    {"DOP", "RD$"}, 
                                                    {"DZD", "DZD"}, 
                                                    {"EEK", "kr"}, 
                                                    {"EGP", "ج.م.‏ "},
                                                    {"ETB", "ETB"}, 
                                                    {"EUR", "€"}, 
                                                    {"GBP", "£"}, 
                                                    {"GEL", "Lari"}, 
                                                    {"GTQ", "Q"}, 
                                                    {"HKD", "HK$"}, 
                                                    {"HNL", "L."}, 
                                                    {"HRK", "kn"}, 
                                                    {"HUF", "Ft"}, 
                                                    {"IDR", "Rp"}, 
                                                    {"ILS", "₪"}, 
                                                    {"INR", "रु"}, 
                                                    {"IQD", "د.ع.‏ "},
                                                    {"IRR", "ريال "},
                                                    {"ISK", "kr."}, 
                                                    {"JMD", "J$"}, 
                                                    {"JOD", "د.ا.‏ "},
                                                    {"JPY", "¥"}, 
                                                    {"KES", "S"}, 
                                                    {"KGS", "сом"}, 
                                                    {"KHR", "៛"}, 
                                                    {"KRW", "₩"}, 
                                                    {"KWD", "د.ك.‏ "},
                                                    {"KZT", "Т"}, 
                                                    {"LAK", "₭"}, 
                                                    {"LBP", "ل.ل.‏ "},
                                                    {"LKR", "රු."}, 
                                                    {"LTL", "Lt"}, 
                                                    {"LVL", "Ls"}, 
                                                    {"LYD", "د.ل.‏ "},
                                                    {"MAD", "د.م.‏ "},
                                                    {"MKD", "ден."}, 
                                                    {"MNT", "₮"}, 
                                                    {"MOP", "MOP"}, 
                                                    {"MVR", "ރ."}, 
                                                    {"MXN", "$"}, 
                                                    {"MYR", "RM"}, 
                                                    {"NIO", "N"}, 
                                                    {"NOK", "kr"}, 
                                                    {"NPR", "रु"}, 
                                                    {"NZD", "$"}, 
                                                    {"OMR", "ر.ع.‏ "},
                                                    {"PAB", "B/."}, 
                                                    {"PEN", "S/."}, 
                                                    {"PHP", "PhP"}, 
                                                    {"PKR", "Rs"}, 
                                                    {"PLN", "zł"}, 
                                                    {"PYG", "Gs"}, 
                                                    {"QAR", "ر.ق.‏ "},
                                                    {"RON", "lei"}, 
                                                    {"RSD", "Din."}, 
                                                    {"RUB", "р."}, 
                                                    {"RWF", "RWF"}, 
                                                    {"SAR", "ر.س.‏ "},
                                                    {"SEK", "kr"}, 
                                                    {"SGD", "$"}, 
                                                    {"SYP", "ل.س.‏ "},
                                                    {"THB", "฿"}, 
                                                    {"TJS", "т.р."}, 
                                                    {"TMT", "m."}, 
                                                    {"TND", "د.ت.‏ "},
                                                    {"TRY", "TL"}, 
                                                    {"TTD", "TT$"}, 
                                                    {"TWD", "NT$"}, 
                                                    {"UAH", "₴"}, 
                                                    {"USD", "$"}, 
                                                    {"UYU", "$U"}, 
                                                    {"UZS", "so'm"}, 
                                                    {"VEF", "Bs. F."}, 
                                                    {"VND", "₫"}, 
                                                    {"XOF", "XOF"}, 
                                                    {"YER", "ر.ي.‏ "},
                                                    {"ZAR", "R"}, 
                                                    {"ZWL", "Z$"} };
}
Marius Bughiu
  • 997
  • 14
  • 32
7

Try this code. Enter 'USD' as CurrencyCode and all other.

public string getCurrencySymbol(string CurrencyCode)    
{
        string symbol = string.Empty;
        CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
        IList Result = new ArrayList();
        foreach (CultureInfo ci in cultures)
        {
            RegionInfo ri = new RegionInfo(ci.LCID);
            if (ri.ISOCurrencySymbol == CurrencyCode)
            {
                symbol = ri.CurrencySymbol;
                return symbol;
            }
        }
        return symbol;

    }
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
1

This answer puts @spender's code, with a minor tweak, in a utility class that attempts to convert three-letter ISO currency codes to their presently-circulating symbols. For efficiency, this class uses an internal cache of all previous requests, also suggested by @spender.

public static class CurrencySymbolMapper {
    /// <summary>An internal cache of previously looked up currencies.</summary>
    private static Dictionary<string, string> _currencySymbolsCache =
        new Dictionary<string, string> (StringComparer.CurrentCultureIgnoreCase);

    public static string TryGetCurrencySymbol(string threeLetterISOAlphabeticCode) {
        // TODO: Enhance to get rid of the silent exception that gets thrown when constructing a new RegionInfo(CultureInfo.LCID) temporary object

        if (threeLetterISOAlphabeticCode.Length != 3) return string.Empty;
        if (_currencySymbolsCache.ContainsKey(threeLetterISOAlphabeticCode))
            return _currencySymbolsCache[threeLetterISOAlphabeticCode];

        string currencySymbolSearchResult = string.Empty;
        try {
            currencySymbolSearchResult =
                CultureInfo.GetCultures(CultureTypes.AllCultures)
                            .Where(c => !c.IsNeutralCulture)
                            .Select(culture => {
                                try { return new RegionInfo(culture.LCID); }
                                catch { return null; } // Ignore this error, but enhance future implementation to get ride of this silent exception
                            })
                            .Where(ri => ri != null && string.Equals(ri.ISOCurrencySymbol, threeLetterISOAlphabeticCode, StringComparison.OrdinalIgnoreCase))
                            .Select(ri => ri.CurrencySymbol)
                            .FirstOrDefault();
        }
        catch (Exception e) {
            // TODO: Handle error
        }

        if (currencySymbolSearchResult == null) currencySymbolSearchResult = string.Empty;

        // Saves both valid and invalid search results, just in case users hammer this method with 
        // the same invalid request many times
        _currencySymbolsCache.Add(threeLetterISOAlphabeticCode, currencySymbolSearchResult);
        return currencySymbolSearchResult;
    }
}
Zaid
  • 11
  • 1
1

With the help of this thread I made a short string extension method

public static string ToCurrencySymbol(this string ISOCurrency)
{            
    RegionInfo region = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(x => new RegionInfo(x.LCID)).FirstOrDefault(p => p.ISOCurrencySymbol == ISOCurrency);
    return region?.ISOCurrencySymbol ?? ISOCurrency;
}
artzuk
  • 133
  • 1
  • 8
  • 2
    `return region?.ISOCurrencySymbol ?? ISOCurrency;` should be changed to `return region?.CurrencySymbol ?? ISOCurrency;` as ISOCurrencySymbol returns the 3 digit currency code – Sarah Apr 08 '20 at 14:20
0
public static string GetCurrencySymbol(string code)  
{  
 System.Globalization.RegionInfo regionInfo = (from culture in System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.InstalledWin32Cultures)  
              where culture.Name.Length > 0 && !culture.IsNeutralCulture  
              let region = new System.Globalization.RegionInfo(culture.LCID)  
              where String.Equals(region.ISOCurrencySymbol, code, StringComparison.InvariantCultureIgnoreCase)  
              select region).First();  

 return regionInfo.CurrencySymbol;  
}
Skorunka František
  • 5,102
  • 7
  • 44
  • 69
0

This is how I have been getting the

DECLARE @CultureCode varchar(10) = 'en-us'

SELECT SUBSTRING(FORMAT(CONVERT(money,0), 'C', @CultureCode ),1,1) CurrencySymbol

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • This will break when the currency symbol is more than 1 char as in many culture/countries. For ex, en-MY in Malaysia uses RM for currency symbol. – Anand Sowmithiran Apr 26 '22 at 06:45