3数字货币代码到货币符号

在C#中,有可能从3个字符的货币代码中获得一个货币符号,例如'£',在这种情况下是'GBP'?

这可能在SQL Server或C#中?

虽然有点蛮力,不是特别优雅,你可以这样做:

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

并使用它如下:

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

如果您预计会采用这种方法,那么最好先build立一个caching:

 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.LCID); } 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); } } 

在写作的时候,在我的机器等等,地图包含以下映射:

 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$ 

.NET有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); // € 

但这需要文化名称,而不是"GBP" 。 据我所知不可能直接从"GBP"等等

同样的信息也可以通过RegionInfo ,以及货币代码:

 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 

我想可以使用它来构build一个从ISO代码到符号的地图。

文化名称列表在这里是可用的。

编辑:好吧,这似乎工作:

 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); } } 

用法:

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

请注意,这当然不会产生一个全面的列表。 尤其是,它不包括被欧元所取代的旧欧元区货币。 我无法看到任何解决方法,但手动添加这样的货币,如果你需要他们,例如SymbolsByCode.Add("FRF", "₣"); 为法国法郎。

RegionInfo类有一个CurrencySymbol属性 ,所以它在C#中是可行的。 如果你想在Sql Server中执行,你也许可以使用C#存储过程。

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

(您需要使用ISO国家/地区代码 )

这不适用于Windows Phone应用程序,因为CultureInfo.GetCultures在平台上不可用(至less现在还没有)。 所以这里是一个快速和肮脏的解决scheme – 在含有所有的文化代码和货币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$"} }; } 

试试这个代码。 input“USD”作为CurrencyCode和所有其他。

 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; } 

这个答案把@ spender的代码在一个实用程序类中进行了小小的调整,试图将三个字母的ISO货币代码转换为当前循环的符号。 为了提高效率,这个类使用所有以前的请求的内部caching,也由@spenderbuild议。

 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; } } 
 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; } 

在这个线程的帮助下,我做了一个简短的string扩展方法

 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; }