Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The code queries the cultures using linq to find a culture matching the given currency code it then uses passes the culture into string.Format and asks it to format as currency (”C”).
public static string FormatCurrency(this decimal amount, string currencyCode)
{
var culture = (from c in CultureInfo.GetCultures(CultureTypes.SpecificCultures)
let r = new RegionInfo(c.LCID)
where r != null
&& r.ISOCurrencySymbol.ToUpper() == currencyCode.ToUpper()
select c).FirstOrDefault();
if (culture == null)
return amount.ToString("0.00");
return string.Format(culture, "{0:C}", amount);
}
Here are the results of calling FormatCurrency for a few different currency codes:
decimal amount = 100;
amount.FormatCurrency("AUD"); //$100.00
amount.FormatCurrency("GBP"); //£100.00
amount.FormatCurrency("EUR"); //100,00 €
amount.FormatCurrency("VND"); //100,00 ₫
amount.FormatCurrency("IRN"); //₹ 100.00