C# numberformatinfo中的多个货币符号

C# numberformatinfo中的多个货币符号,c#,localization,number-formatting,cultureinfo,C#,Localization,Number Formatting,Cultureinfo,在编写一段代码时,我遇到了这样一个问题:使用Numberformatinfo,我必须同时为一个国家编写两个货币符号 台湾现在使用TWD和起。所以他们把他们的货币写为新台币23900起 但仅仅使用NumberformatInfo,我无法同时放置两个货币符号 public NumberFormatInfo GetCurrencyFormat(string countryCode, string languageCode) {var cultureInfo = GetCultureIn

在编写一段代码时,我遇到了这样一个问题:使用
Numberformatinfo
,我必须同时为一个国家编写两个货币符号

台湾现在使用
TWD
。所以他们把他们的货币写为新台币23900起

但仅仅使用NumberformatInfo,我无法同时放置两个货币符号

    public NumberFormatInfo GetCurrencyFormat(string countryCode, string languageCode)
    {var cultureInfo = GetCultureInfo(countryCode, languageCode);

        var currencyFormat = GetCurrencyFormat(cultureInfo);
        return currencyFormat;
    }

在这里,我可以更改符号,但只能更改为上面提到的其中一个符号,它可以放在金额之前或之后。

恐怕只有一种方法,如何做到这一点。您需要使用自定义格式化程序实现自定义类型

似乎不支持两种货币符号/快捷方式和/或四种预定义格式之一(请参阅:)

简单的版本可以是这样的

using System;
using System.Globalization;

namespace TwoCurrencySymbols
{
  internal sealed class Currency : IFormattable
  {
    private readonly IFormattable value;

    public Currency(IFormattable myValue)
    {
      value = myValue;
    }

    public string ToString(string format, IFormatProvider formatProvider)
    {
      if (format == "C")
      {
        return ("EUR " + value.ToString(format, formatProvider));
      }

      return value.ToString(format, formatProvider);
    }
  }

  internal static class Program
  {
    private static void Main()
    {
      Console.WriteLine(string.Format(CultureInfo.CurrentCulture, "{0:C}", new Currency(1)));
    }
  }
}

该示例是为欧元(我的语言环境)构建的。在实际实现中,您需要确定是否应该更改格式,例如,如果
if((format==“C”)&&IsTaiwan(formatProvider))

文化信息可能会出现更多问题。看到这篇文章(包括评论和回答):我很高兴我的回答帮助了你。不幸的是,我没有时间做一个更好的例子。恐怕仅从格式提供程序进行国家/地区检测并不是那么简单。而且像
C2
这样的特定格式也存在问题。