如何获得特定的文化货币模式 [英] How to get specific culture currency pattern

查看:5

问题描述

我如何获得特定文化的货币模式?

例如:

而不是使用:

string.Format("{0:c}", 345.10)

我想用这个:

string.Format("#.##0,00 €;-#.##0,00 €", 345.10);

但是我如何为我的应用程序需要的每种文化获取模式字符串(例如#.##0,00 €;-#.##0,00 €")?

我不能使用{0:c}"模式,因为如果用户切换语言,货币应该是相同的.

解决方案

一个 CultureInfo 包含一个 NumberFormatInfo 并且这个类描述(除其他外)如何格式化货币那种特殊的文化.

特别是您可以使用 CurrencyPositivePatternCurrencyNegativePattern 来确定货币符号是放在金额之前还是之后,当然还有 CurrencySymbol 来确定获取正确的货币符号.当使用 C 格式说明符时,.NET 会使用所有这些信息.

您可以在 MSDN 上阅读有关 NumberFormatInfo 类的更多信息.p>

下面的代码演示了正确格式化货币所需的一些步骤.它仅使用 CurrencySymbolCurrencyPositivePatternCurrencyDecimalDigits,因此不完整:

var 数量 = 123.45M;varcultureInfo = CultureInfo.GetCultureInfo("da-DK");var numberFormat =cultureInfo.NumberFormat;字符串格式化金额 = null;if (amount >= Decimal.Zero) {字符串模式 = null;开关(numberFormat.CurrencyPositivePattern){案例0:模式 = "{0}{1:N" + numberFormat.CurrencyDecimalDigits + "}";休息;情况1:模式 = "{1:N" + numberFormat.CurrencyDecimalDigits + "}{0}";休息;案例2:模式 = "{0} {1:N" + numberFormat.CurrencyDecimalDigits + "}";休息;案例3:模式 = "{1:N" + numberFormat.CurrencyDecimalDigits + "} {0}";休息;}formattedAmount = String.Format(cultureInfo, pattern, numberFormat.CurrencySymbol, amount);}别的 {//...}Console.WriteLine(formattedAmount);

当然你可以简单地使用:

var 数量 = 123.45M;varcultureInfo = CultureInfo.GetCultureInfo("da-DK");var formattedAmount = String.Format(cultureInfo, "{0:C}", amount);Console.WriteLine(formattedAmount);

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.

解决方案

A CultureInfo contains a NumberFormatInfo and this class describes (among other things) how to format currency for that particular culture.

In particular you can use CurrencyPositivePattern and CurrencyNegativePattern to determine if the currency symbol is placed before or after the amount and of course CurrencySymbol to get the correct currency symbol. All this information is used by .NET when the C 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 and CurrencyDecimalDigits and thus is incomplete:

var amount = 123.45M;
var cultureInfo = CultureInfo.GetCultureInfo("da-DK");

var numberFormat = cultureInfo.NumberFormat;
String formattedAmount = null;
if (amount >= Decimal.Zero) {
  String pattern = null;
  switch (numberFormat.CurrencyPositivePattern) {
    case 0:
      pattern = "{0}{1:N" + numberFormat.CurrencyDecimalDigits + "}";
      break;
    case 1:
      pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "}{0}";
      break;
    case 2:
      pattern = "{0} {1:N" + numberFormat.CurrencyDecimalDigits + "}";
      break;
    case 3:
      pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "} {0}";
      break;
  }
  formattedAmount = String.Format(cultureInfo, pattern, numberFormat.CurrencySymbol, amount);
}
else {
  // ...
}

Console.WriteLine(formattedAmount);

Of course you could simply use:

var amount = 123.45M;
var cultureInfo = CultureInfo.GetCultureInfo("da-DK");
var formattedAmount = String.Format(cultureInfo, "{0:C}", amount);
Console.WriteLine(formattedAmount);

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