根据货币代码将货币格式设置为货币 [英] Format decimal as currency based on currency code

查看:173
本文介绍了根据货币代码将货币格式设置为货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数额表和货币代码(美元,日元,加元,欧元等)的收费表,我正在寻找正确格式化货币的最简单的方法。使用我的本地文化代码(美国),并采取我的decimal.ToString(c)得到我0.00美元的输出,但我想正确的货币符号和基于代码小数的数量。

这个库是否存在?我当然可以写一个switch语句和每个人的自定义规则,但是以为这一定是以前做过的。



更新:



我修改了Jon B的示例代码,如下所示:

$ p $ static $ IDictionary< string,string> GetCurrencyFormatStrings()
{
var result = new Dictionary< string,string>();

foreach(CultureInfo.GetCultures(CultureTypes.AllCultures)中的var ci)
{
尝试
{
var ri = new RegionInfo(ci.Name );
结果[ri.ISOCurrencySymbol] =
string.Format({0}#,#0. {1};({0}#,#0. {1}),
ri.CurrencySymbol,
新字符串('0',
i.NumberFormat.CurrencyDecimalDigits));
}
catch {}
}

返回结果;





$ p $这个让我简单的去Amount.ToString(Currency [JPY] ),格式将在我的本地上下文中输出逗号分隔符,但自动将正确的货币符号和小数位置。



让我知道是否有人有清洁这样做的方法,或者我会标记乔恩的答案是正确的。

解决方案

你可以建立一个字典,从ISO货币符号(USD)为货币符号($):

pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
var symbols = GetCurrencySymbols();

Console.WriteLine({0} {1:0.00},符号[USD],1.5M);
Console.WriteLine({0} {1:0.00},符号[JPY],1.5M);

Console.ReadLine();
}

static IDictionary< string,string> GetCurrencySymbols()
{
var result = new Dictionary< string,string>();

foreach(CultureInfo.GetCultures(CultureTypes.AllCultures)中的var ci)
{
尝试
{
var ri = new RegionInfo(ci.Name );
结果[ri.ISOCurrencySymbol] = ri.CurrencySymbol;
}
catch {}
}

返回结果;



$ b $ p
$ b

这是基本的想法,你需要调整以适应你的需要。

请注意,您当然可以使用 CultureInfo 类转换为特定文化的字符串,但是作为在阿列克谢的评论中指出,这将导致每个字符串有点不同(如1.00与1.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.

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.

解决方案

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.

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.

这篇关于根据货币代码将货币格式设置为货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆