I have a table of charges with the amount, and the currency code (USD, JPY, CAD, EUR etc.), and am looking for the easiest way to properly format the currency. Using my local culture code (USA) and taking my decimal.ToString("c") gets me $0.00 output, but I'd like the correct currency sign and number of decimals based on the code.
Do any libraries exist for this? I can of course write up a switch statement and custom rules for each one, but thought this must have been done before.
Update:
I've modified Jon B's sample code as follows
static IDictionary<string, string> GetCurrencyFormatStrings()
{
var result = new Dictionary<string, string>();
foreach (var ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
try
{
var ri = new RegionInfo(ci.Name);
result[ri.ISOCurrencySymbol] =
string.Format("{0}#,#0.{1};({0}#,#0.{1})",
ri.CurrencySymbol,
new string('0',
i.NumberFormat.CurrencyDecimalDigits));
}
catch { }
}
return result;
}
This allows me to simply go Amount.ToString(Currency["JPY"]), and the format will output the comma separator in my local context, but put the correct currency symbol and decimal places in automatically.
Let me know if anyone has a cleaner way of doing this, or I will mark Jon's answer as correct shortly.
It is composed of the country code ( US ), followed by the letter "D" for "dollar." Write the dollar figure first, followed by a non-breaking space and the code: 350 000 USD. 125,00 CAD = 121,22 USD , selon un taux de change de 0,9697 [ CAD is the international currency code for the Canadian dollar.]
On the Home tab, click the Dialog Box Launcher next to Number. Tip: You can also press Ctrl+1 to open the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Currency or Accounting. In the Symbol box, click the currency symbol that you want.
By default, cells formatted as currency display two decimal places. You can change this setting so cells display as many decimal places as you type in them, or so all cells display the same number of decimal places.
According to the ISO standard, currency precision can range from zero decimals to three decimals.
public static class DecimalExtension
{
private static readonly Dictionary<string, CultureInfo> ISOCurrenciesToACultureMap =
CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(c => new {c, new RegionInfo(c.LCID).ISOCurrencySymbol})
.GroupBy(x => x.ISOCurrencySymbol)
.ToDictionary(g => g.Key, g => g.First().c, StringComparer.OrdinalIgnoreCase);
public static string FormatCurrency(this decimal amount, string currencyCode)
{
CultureInfo culture;
if (ISOCurrenciesToACultureMap.TryGetValue(currencyCode, out culture))
return string.Format(culture, "{0:C}", amount);
return amount.ToString("0.00");
}
}
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
Should be done by passing the CultureInfo
:
CultureInfo ci = new CultureInfo("en-GB");
amount.ToString("C", ci);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With