Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...