3 Digit currency code to currency symbol
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
.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.