3 Digit currency code to currency symbol

前端 未结 9 1176
逝去的感伤
逝去的感伤 2020-12-04 10:19

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

相关标签:
9条回答
  • 2020-12-04 10:56

    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;
    }
    
    0 讨论(0)
  • 2020-12-04 10:57
    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;  
    }
    
    0 讨论(0)
  • 2020-12-04 11:00

    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;
    
        }
    
    0 讨论(0)
提交回复
热议问题