Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3 Digit currency code to currency symbol

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#?

like image 936
Sachin Kainth Avatar asked Sep 11 '12 16:09

Sachin Kainth


People also ask

What is a 3 letter currency code?

The first two letters of the ISO 4217 three-letter code are the same as the code for the country name, and, where possible, the third letter corresponds to the first letter of the currency name. For example: The US dollar is represented as USD – the US coming from the ISO 3166 country code and the D for dollar.

Do all currency codes have 3 letters?

The official ISO 4217 standard specifies three-letter ("Alpha-3") codes for currencies worldwide. The first two letters of these codes are usually identical with the two-letter ISO 3166-1 Alpha-2 country codes, which are well-known by internet users, as they are used for country domain suffixes.

How do you code currencies?

The numeric code assigned to a currency by ISO 4217 is usually the same as the numeric code assigned to the corresponding country by ISO 3166-1. For example, USD (United States dollar) has numeric code 840 which is also the numeric code for the US (United States).

How many ISO currency codes are there?

The following table provides a list of valid 4217 letter codes.


1 Answers

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 
like image 108
spender Avatar answered Sep 19 '22 15:09

spender