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.