在.NET货币格式 [英] Currency formatting in .NET

查看:238
本文介绍了在.NET货币格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让在.NET框架如何货币格式作品的抓地力。据我了解,Thread.CurrentCulture.NumberFormatInfo.CurrencySymbol包含了本地文化的货币符号。



不过,在我看来,在现实世界中有没有一个具体的文化和货币符号之间有明显的1对1的关系。举例来说,我可能位于英国,但我我的账单发票欧元。或者我可能住在冰岛和美国供应商在收到美元的发票。或者我可以住在瑞典,但我的银行帐户使用欧元。我认识到,在某些情况下,你可能只是想假设当地的货币是用的人,但往往事实并非如此。



在这种情况下,我会克隆的CultureInfo和手动设置货币符号的克隆,然后格式化金额时所使用的克隆?即使货币符号是无效的,我认为它仍然是有意义的使用的NumberFormatInfo的其他属性,如CurrencyDecimalSeparator。


解决方案

当然可以。我已经使用基于由马特·韦伯博客文章做了。下面是一个使用你的文化的货币格式(小数位等)的例子,但使用的货币符号,并适合于给定货币代码(所以一万日元的EN-US区域性小数位数将被格式化为¥1,000,000个)。



您当然可以修改它来挑选哪些当前区域性和货币文化的特性都将保留。



<预类=郎-CS prettyprint-覆盖> 公共静态的NumberFormatInfo GetCurrencyFormatProviderSymbolDecimals(字符串CURRENCYCODE)
{
如果(String.IsNullOrWhiteSpace(CURRENCYCODE))
返回NumberFormatInfo.CurrentInfo;


VAR currencyNumberFormat =(从CultureInfo.GetCultures(CultureTypes.SpecificCultures文化)
让区域=新RegionInfo(culture.LCID)
,其中String.Equals( region.ISOCurrencySymbol,CURRENCYCODE,
StringComparison.InvariantCultureIgnoreCase)
选择culture.NumberFormat)。首先();

//需要克隆()一个浅拷贝在这里,因为的GetInstance()返回一个只读的NumberFormatInfo
VAR desiredNumberFormat =(的NumberFormatInfo)NumberFormatInfo.GetInstance(CultureInfo.CurrentCulture).Clone ();
desiredNumberFormat.CurrencyDecimalDigits = currencyNumberFormat.CurrencyDecimalDigits;
desiredNumberFormat.CurrencySymbol = currencyNumberFormat.CurrencySymbol;

返回desiredNumberFormat;
}


I'm trying to get a grip on how currency formatting works in the .NET framework. As I understand it, Thread.CurrentCulture.NumberFormatInfo.CurrencySymbol contains the local culture's currency symbol.

But as I see it, in the real world there's not a clear 1-to-1 relation between a specific culture and the currency symbol. For instance, I may be located in UK but I bill my invoices in Euro. Or I may live in Iceland and receive invoices from US suppliers in USD. Or I may live in Sweden but my bank account uses Euro. I realize that in some cases you may just want to assume that the local currency is the one to use, but often this isn't the case.

In these cases, would I clone the CultureInfo and set the currency symbol manually on the clone and then use the clone when formatting an amount? Even if the currency symbol is not valid, I think that it would still make sense to use other properties of the NumberFormatInfo, such as CurrencyDecimalSeparator.

解决方案

Absolutely. I've done it using a technique based on a blog post by Matt Weber. Here's an example that uses your culture's format for currency (decimal places, etc.), but uses the currency symbol and number of decimal places appropriate for a given currency code (so one million Yen in the en-US culture would be formatted as ¥1,000,000).

You can, of course, modify it to pick and choose which properties of the current culture and currency's culture are retained.

public static NumberFormatInfo GetCurrencyFormatProviderSymbolDecimals(string currencyCode)
{
    if (String.IsNullOrWhiteSpace(currencyCode))
        return NumberFormatInfo.CurrentInfo;


    var currencyNumberFormat = (from culture in CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                                let region = new RegionInfo(culture.LCID)
                                where String.Equals(region.ISOCurrencySymbol, currencyCode,
                                                    StringComparison.InvariantCultureIgnoreCase)
                                select culture.NumberFormat).First();

    //Need to Clone() a shallow copy here, because GetInstance() returns a read-only NumberFormatInfo
    var desiredNumberFormat = (NumberFormatInfo)NumberFormatInfo.GetInstance(CultureInfo.CurrentCulture).Clone();
    desiredNumberFormat.CurrencyDecimalDigits = currencyNumberFormat.CurrencyDecimalDigits;
    desiredNumberFormat.CurrencySymbol = currencyNumberFormat.CurrencySymbol;

    return desiredNumberFormat;
}

这篇关于在.NET货币格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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