如何获得特定文化货币图案

发布于 2024-11-27 13:03:15 字数 304 浏览 0 评论 0原文

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

例如:

而不是使用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

看海 2024-12-04 13:03:15

CultureInfo 包含 NumberFormatInfo,此类描述(除其他外)如何针对特定文化设置货币格式。

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

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

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

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

当然,您可以简单地使用:

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

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);
记忆で 2024-12-04 13:03:15

我认为您要问的是如何更改货币符号但保留特定于文化的格式。您可以通过获取当前 NumberFormatInfo 的副本并修改 CurrencySymbol 属性来完成此操作:

Thread.CurrentThread.CurrentCulture = new CultureInfo("de");
// pretend we are german

var nfi = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
nfi.CurrencySymbol = "$$";
Console.WriteLine(string.Format(nfi,"{0:c}",345.10));

这将输出:

345,10 $$

不更改它输出的 CurrentCulture (为我):

$$345.10

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 the CurrencySymbol property:

Thread.CurrentThread.CurrentCulture = new CultureInfo("de");
// pretend we are german

var nfi = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
nfi.CurrencySymbol = "$$";
Console.WriteLine(string.Format(nfi,"{0:c}",345.10));

This will output:

345,10 $$

Without changing the CurrentCulture it outputs (for me):

$$345.10
美人如玉 2024-12-04 13:03:15

您需要使用以下方法格式化您的货币/双精度:

money.ToString("C", culture);

困难的部分实际上是根据 ISO 代码获取正确的区域性。我不知道你如何跟踪你需要的文化。请记住,这只是您的资金的格式化,而不是转换到不同的货币/文化!

更多详细信息:

ISOCurrencySymbol 是 RegionInfo 的一部分,您可以基于 CultureInfo 创建它,您可以从当前线程的区域性设置中检索它。您应该创建一个单例,它实现一个字典以从 ISOCurrencyCode 转换为 CultureInfo。

You need to format your currency/double using:

money.ToString("C", culture);

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.

风透绣罗衣 2024-12-04 13:03:15

适用于所有数字格式的快速而肮脏的方法是:

var culture = CultureInfo.GetCultureInfo("el-GR");
var numberFormat = (NumberFormatInfo)culture.NumberFormat.Clone();
numberFormat.CurrencySymbol = "€";  // Force the currency symbol regardless of culture
var specifier = "C";                // Or any other format specifier
var positivePattern = 1110.ToString(specifier, numberFormat).Replace('1', '#');
var negativePattern = (-1110).ToString(specifier, numberFormat).Replace('1', '#');
var pattern = positivePattern + ";" + negativePattern;

在本例中,模式等于“#.##0,00 €;-#.##0,00 €”。这避免了试图找出所有排列的许多麻烦。我很欣赏这个问题,因为它帮助并迫使我找到一个更简单的答案。

Quick and dirty approach that works for all number formats is:

var culture = CultureInfo.GetCultureInfo("el-GR");
var numberFormat = (NumberFormatInfo)culture.NumberFormat.Clone();
numberFormat.CurrencySymbol = "€";  // Force the currency symbol regardless of culture
var specifier = "C";                // Or any other format specifier
var positivePattern = 1110.ToString(specifier, numberFormat).Replace('1', '#');
var negativePattern = (-1110).ToString(specifier, numberFormat).Replace('1', '#');
var pattern = positivePattern + ";" + negativePattern;

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.

狼性发作 2024-12-04 13:03:15

您是否尝试过使用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.

肩上的翅膀 2024-12-04 13:03:15

下面的测试说明了如何实现此目的:

    [Test]
    public void DisplayEurosInGreeceAndEngland()
    {
        var val = 125.22m;
        Thread.CurrentThread.CurrentCulture
            = Thread.CurrentThread.CurrentUICulture
              = new CultureInfo("el-GR");

        Console.WriteLine(string.Format("{0:n} €", val));

        Thread.CurrentThread.CurrentCulture
            = Thread.CurrentThread.CurrentUICulture
              = new CultureInfo("en-GB");

        Console.WriteLine(string.Format("{0:n} €", val));
    }

通过使用当前所选区域性的标准十进制表示法,您可以显示跳过货币的任何给定值,您可以单独处理这些值。

The test below illustrates how you can achieve this:

    [Test]
    public void DisplayEurosInGreeceAndEngland()
    {
        var val = 125.22m;
        Thread.CurrentThread.CurrentCulture
            = Thread.CurrentThread.CurrentUICulture
              = new CultureInfo("el-GR");

        Console.WriteLine(string.Format("{0:n} €", val));

        Thread.CurrentThread.CurrentCulture
            = Thread.CurrentThread.CurrentUICulture
              = new CultureInfo("en-GB");

        Console.WriteLine(string.Format("{0:n} €", val));
    }

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.

淡紫姑娘! 2024-12-04 13:03:15

对于正数和负数,一可以使用下面的代码片段进行文化

class Program
{
    static void Main(string[] args)
    {
        List<string> cultures = new List<string> { "ca-ES", "co-FR", "cs-CZ", "cy-GB", "da-DK", "de-AT", "de-CH", "de-DE", "de-LI", "de-LU", "dsb-DE", "en-US", "en-GB" };

        var amount = -16.34M;

        foreach (var c in cultures)
        {
            var cultureInfo = CultureInfo.GetCultureInfo(c);

            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 if (amount < Decimal.Zero)
            {
                String pattern = null;
                switch (numberFormat.CurrencyNegativePattern)
                {
                    case 0:
                        pattern = "({0}{1:N" + numberFormat.CurrencyDecimalDigits + "})";
                        break;
                    case 1:
                        pattern = numberFormat.NegativeSign + "{0}{1:N" + numberFormat.CurrencyDecimalDigits + "}";
                        break;
                    case 2:
                        pattern = "{0}" + numberFormat.NegativeSign + "{1:N" + numberFormat.CurrencyDecimalDigits + "}";
                        break;
                    case 3:
                        pattern = "{0}{1:N" + numberFormat.CurrencyDecimalDigits + "}" + numberFormat.NegativeSign;
                        break;
                    case 4:
                        pattern = "({1:N" + numberFormat.CurrencyDecimalDigits + "}{0})";
                        break;
                    case 5:
                        pattern = numberFormat.NegativeSign + "{1:N" + numberFormat.CurrencyDecimalDigits + "}{0}";
                        break;
                    case 6:
                        pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "}" + numberFormat.NegativeSign + "{0}";
                        break;
                    case 7:
                        pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "}{0}" + numberFormat.NegativeSign;
                        break;
                    case 8:
                        pattern = numberFormat.NegativeSign + "{1:N" + numberFormat.CurrencyDecimalDigits + "} {0}";
                        break;
                    case 9:
                        pattern = numberFormat.NegativeSign + "{0} {1:N" + numberFormat.CurrencyDecimalDigits + "}";
                        break;
                    case 10:
                        pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "} {0}" + numberFormat.NegativeSign;
                        break;
                    case 11:
                        pattern = "{0} {1:N" + numberFormat.CurrencyDecimalDigits + "}" + numberFormat.NegativeSign;
                        break;
                    case 12:
                        pattern = "{0}" + " " + numberFormat.NegativeSign + "{1:N" + numberFormat.CurrencyDecimalDigits + "}";
                        break;
                    case 13:
                        pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "}" + numberFormat.NegativeSign + " " + "{0}";
                        break;
                    case 14:
                        pattern = "({0} {1:N" + numberFormat.CurrencyDecimalDigits + "})";
                        break;
                    case 15:
                        pattern = "({1:N" + numberFormat.CurrencyDecimalDigits + "} {0})";
                        break;
                }
                formattedAmount = String.Format(cultureInfo, pattern, numberFormat.CurrencySymbol, amount * -1);
            }

            Console.WriteLine(formattedAmount);
        }

        Console.ReadKey();

    }
}

For Positive and negative number one can use below code snippet for culture

class Program
{
    static void Main(string[] args)
    {
        List<string> cultures = new List<string> { "ca-ES", "co-FR", "cs-CZ", "cy-GB", "da-DK", "de-AT", "de-CH", "de-DE", "de-LI", "de-LU", "dsb-DE", "en-US", "en-GB" };

        var amount = -16.34M;

        foreach (var c in cultures)
        {
            var cultureInfo = CultureInfo.GetCultureInfo(c);

            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 if (amount < Decimal.Zero)
            {
                String pattern = null;
                switch (numberFormat.CurrencyNegativePattern)
                {
                    case 0:
                        pattern = "({0}{1:N" + numberFormat.CurrencyDecimalDigits + "})";
                        break;
                    case 1:
                        pattern = numberFormat.NegativeSign + "{0}{1:N" + numberFormat.CurrencyDecimalDigits + "}";
                        break;
                    case 2:
                        pattern = "{0}" + numberFormat.NegativeSign + "{1:N" + numberFormat.CurrencyDecimalDigits + "}";
                        break;
                    case 3:
                        pattern = "{0}{1:N" + numberFormat.CurrencyDecimalDigits + "}" + numberFormat.NegativeSign;
                        break;
                    case 4:
                        pattern = "({1:N" + numberFormat.CurrencyDecimalDigits + "}{0})";
                        break;
                    case 5:
                        pattern = numberFormat.NegativeSign + "{1:N" + numberFormat.CurrencyDecimalDigits + "}{0}";
                        break;
                    case 6:
                        pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "}" + numberFormat.NegativeSign + "{0}";
                        break;
                    case 7:
                        pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "}{0}" + numberFormat.NegativeSign;
                        break;
                    case 8:
                        pattern = numberFormat.NegativeSign + "{1:N" + numberFormat.CurrencyDecimalDigits + "} {0}";
                        break;
                    case 9:
                        pattern = numberFormat.NegativeSign + "{0} {1:N" + numberFormat.CurrencyDecimalDigits + "}";
                        break;
                    case 10:
                        pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "} {0}" + numberFormat.NegativeSign;
                        break;
                    case 11:
                        pattern = "{0} {1:N" + numberFormat.CurrencyDecimalDigits + "}" + numberFormat.NegativeSign;
                        break;
                    case 12:
                        pattern = "{0}" + " " + numberFormat.NegativeSign + "{1:N" + numberFormat.CurrencyDecimalDigits + "}";
                        break;
                    case 13:
                        pattern = "{1:N" + numberFormat.CurrencyDecimalDigits + "}" + numberFormat.NegativeSign + " " + "{0}";
                        break;
                    case 14:
                        pattern = "({0} {1:N" + numberFormat.CurrencyDecimalDigits + "})";
                        break;
                    case 15:
                        pattern = "({1:N" + numberFormat.CurrencyDecimalDigits + "} {0})";
                        break;
                }
                formattedAmount = String.Format(cultureInfo, pattern, numberFormat.CurrencySymbol, amount * -1);
            }

            Console.WriteLine(formattedAmount);
        }

        Console.ReadKey();

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文