[英]Format decimal as currency based on currency code
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.
使用我的当地文化代码(美国)并使用我的decimal.ToString(“ c”)可以得到$ 0.00的输出,但是我想要基于代码的正确货币符号和小数位数。
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.
我当然可以为每个语句编写一个switch语句和自定义规则,但是我认为这必须已经完成。
Update: 更新:
I've modified Jon B's sample code as follows 我已经修改了Jon B的示例代码,如下所示
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. 这使我可以简单地转到Amount.ToString(Currency [“ JPY”]),该格式将在我的本地上下文中输出逗号分隔符,但会自动将正确的货币符号和小数位放入。
Let me know if anyone has a cleaner way of doing this, or I will mark Jon's answer as correct shortly. 让我知道是否有人有更清洁的方法,否则我会在短期内将乔恩的答案标记为正确。
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: 这是为几种不同的货币代码调用FormatCurrency的结果:
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
来完成:
CultureInfo ci = new CultureInfo("en-GB");
amount.ToString("C", ci);
You could build a dictionary to go from ISO currency symbol (USD) to currency symbol ($): 您可以构建一个字典,将ISO货币符号(USD)转换为货币符号($):
static void Main(string[] args)
{
var symbols = GetCurrencySymbols();
Console.WriteLine("{0}{1:0.00}", symbols["USD"], 1.5M);
Console.WriteLine("{0}{1:0.00}", symbols["JPY"], 1.5M);
Console.ReadLine();
}
static IDictionary<string, string> GetCurrencySymbols()
{
var result = new Dictionary<string, string>();
foreach (var ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
try
{
var ri = new RegionInfo(ci.Name);
result[ri.ISOCurrencySymbol] = ri.CurrencySymbol;
}
catch { }
}
return result;
}
That's the basic idea, you'll need to tweak that to suit your needs. 这是基本思想,您需要对其进行调整以适合您的需求。
Note that you can certainly use the CultureInfo
class to convert to a string for a specific culture, but as noted in Alexei's comment, that will cause each string to be a little different (like 1.00 vs 1,00). 请注意,您当然可以使用
CultureInfo
类将其转换为特定区域性的字符串,但是正如Alexei的注释所指出的,这将导致每个字符串略有不同(例如1.00 vs 1,00)。 Whichever you use depends on your needs. 使用哪种取决于您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.