Format decimal as currency based on currency code(根据货币代码将小数格式化为货币)
问题描述
我有一张包含金额和货币代码(美元、日元、加元、欧元等)的收费表,我正在寻找正确格式化货币的最简单方法.使用我的本地文化代码(美国)并使用我的 decimal.ToString("c") 得到 0.00 美元的输出,但我想要基于代码的正确货币符号和小数位数.
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.
有没有为此而存在的库?我当然可以为每一个写一个 switch 语句和自定义规则,但我想这一定是以前做过的.
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.
更新:
我将 Jon B 的示例代码修改如下
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;
}
这使我可以简单地转到 Amount.ToString(Currency["JPY"]),格式将在我的本地上下文中输出逗号分隔符,但会自动输入正确的货币符号和小数位.
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.
如果有人有更简洁的方法,请告诉我,否则我会尽快将 Jon 的答案标记为正确.
Let me know if anyone has a cleaner way of doing this, or I will mark Jon's answer as correct shortly.
推荐答案
你可以建立一个字典来从 ISO 货币符号 (USD) 到货币符号 ($):
You could build a dictionary to go from ISO currency symbol (USD) to currency symbol ($):
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.
请注意,您当然可以使用 CultureInfo
类转换为特定文化的字符串,但正如 Alexei 的评论中所述,这将导致每个字符串略有不同(例如 1.00 vs1,00).使用哪种取决于您的需求.
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). Whichever you use depends on your needs.
这篇关于根据货币代码将小数格式化为货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:根据货币代码将小数格式化为货币
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01