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

### 问题描述

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

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

``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:

``````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.

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