如何获得特定文化货币图案
如何获得特定文化的货币模式?
例如:
而不是使用:
string.Format("{0:c}", 345.10)
我想使用这个:
string.Format("#.##0,00 €;-#.##0,00 €", 345.10);
但是我如何获取我的应用程序需要的每种文化的模式字符串(如“#.##0,00 €;-#.##0,00 €”) ?
我无法使用“{0:c}”模式,因为如果用户切换语言,货币应该是相同的。
How do i get the currency pattern for a specific culture?
For Example:
Instead of using:
string.Format("{0:c}", 345.10)
I want to use this:
string.Format("#.##0,00 €;-#.##0,00 €", 345.10);
But how do i get the pattern string (like "#.##0,00 €;-#.##0,00 €") for each culture my application needs?
I cant use the "{0:c}" pattern because if the user switches the language the currency should be the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
CultureInfo
包含NumberFormatInfo
,此类描述(除其他外)如何针对特定文化设置货币格式。特别是,您可以使用
CurrencyPositivePattern
和CurrencyNegativePattern
来确定货币符号是放置在金额之前还是之后,当然还可以使用CurrencySymbol
来获得正确的值货币符号。当使用C
格式说明符时,.NET 将使用所有这些信息。您可以在 MSDN 上阅读有关 NumberFormatInfo 类 的更多信息。
下面的代码演示了正确设置货币格式所需的一些步骤。它仅使用
CurrencySymbol
、CurrencyPositivePattern
和CurrencyDecimalDigits
,因此是不完整的:当然,您可以简单地使用:
A
CultureInfo
contains aNumberFormatInfo
and this class describes (among other things) how to format currency for that particular culture.In particular you can use
CurrencyPositivePattern
andCurrencyNegativePattern
to determine if the currency symbol is placed before or after the amount and of courseCurrencySymbol
to get the correct currency symbol. All this information is used by .NET when theC
format specifier is used.You can read more about the NumberFormatInfo class on MSDN.
The code below demonstrates some of the steps required to format currency properly. It only uses
CurrencySymbol
,CurrencyPositivePattern
andCurrencyDecimalDigits
and thus is incomplete:Of course you could simply use:
我认为您要问的是如何更改货币符号但保留特定于文化的格式。您可以通过获取当前
NumberFormatInfo
的副本并修改CurrencySymbol
属性来完成此操作:这将输出:
不更改它输出的
CurrentCulture
(为我):I think what you're asking is how to change the currency symbol but keep the culture-specific formatting. You can do this by getting a copy of the current
NumberFormatInfo
and modifying theCurrencySymbol
property:This will output:
Without changing the
CurrentCulture
it outputs (for me):您需要使用以下方法格式化您的货币/双精度:
困难的部分实际上是根据 ISO 代码获取正确的区域性。我不知道你如何跟踪你需要的文化。请记住,这只是您的资金的格式化,而不是转换到不同的货币/文化!
更多详细信息:
ISOCurrencySymbol 是 RegionInfo 的一部分,您可以基于 CultureInfo 创建它,您可以从当前线程的区域性设置中检索它。您应该创建一个单例,它实现一个字典以从 ISOCurrencyCode 转换为 CultureInfo。
You need to format your currency/double using:
The hard part is actually getting the right culture based on the ISO code. I do not know how you keep track of the culture you need. Keep in mind this is simply the formatting of your money, not conversion to different currencies/cultures!
More detail:
ISOCurrencySymbol is a part of RegionInfo, which you can create based on CultureInfo, which you can retrieve from your current thread's culture settings. You should create a singleton which implements a dictionary to convert from ISOCurrencyCode to CultureInfo.
适用于所有数字格式的快速而肮脏的方法是:
在本例中,模式等于“#.##0,00 €;-#.##0,00 €”。这避免了试图找出所有排列的许多麻烦。我很欣赏这个问题,因为它帮助并迫使我找到一个更简单的答案。
Quick and dirty approach that works for all number formats is:
In this case, pattern equals "#.##0,00 €;-#.##0,00 €". This avoids a lot of headaches trying to figure out all of the permutations. I appreciate the question being asked, as it helped and forced me to find an easier answer.
您是否尝试过使用
string.Format("{0:N2} €", 345.10)
?这应该在用户文化中格式化为小数点后两位,后跟空格和欧元符号。Have you tried using
string.Format("{0:N2} €", 345.10)
? This should format to 2 decimal places in the users culture followed by a space and the euro symbol.下面的测试说明了如何实现此目的:
通过使用当前所选区域性的标准十进制表示法,您可以显示跳过货币的任何给定值,您可以单独处理这些值。
The test below illustrates how you can achieve this:
By using the standard decimal notation from the currently selected culture, you can display any given value skipping the currency, which you can treat separately.
对于正数和负数,一可以使用下面的代码片段进行文化
For Positive and negative number one can use below code snippet for culture