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

查看:88
本文介绍了如何获得特定的文化格局的货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何得到一个特定的文化货币格局

How do i get the currency pattern for a specific culture?

例如:

而不是使用:

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

我想用这样的:

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



但我如何获得模式字符串(如### 0,00€; - ?)为每一种文化我的应用需求。

But how do i get the pattern string (like "#.##0,00 €;-#.##0,00 €") for each culture my application needs?

我不能使用### 0,00€{0:C}的格局,因为如果用户切换语言货币应该是一样的。

I cant use the "{0:c}" pattern because if the user switches the language the currency should be the same.

推荐答案

A 的CultureInfo 包含的NumberFormatInfo 这个类描述(除其他事项外)如何为特定的文化格式化货币。

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

在特别的你可以使用 CurrencyPositivePattern CurrencyNegativePattern 来确定货币符号放置前或后的金额,当然还有 CURRENCYSYMBOL 来得到正确的货币符号。所有这些信息都使用时,使用了 C 格式说明.NET。

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.

您可以阅读更多有关在MSDN上的NumberFormatInfo类

You can read more about the NumberFormatInfo class on MSDN.

下面的代码演示了一些正确格式的货币所需的步骤。它只使用 CURRENCYSYMBOL CurrencyPositivePattern CurrencyDecimalDigits 并因此不完整的:

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);

这篇关于如何获得特定的文化格局的货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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