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