numberformatinfo 中的多个货币符号

Multiple Currency symbols in numberformatinfo

在写一段代码时,我遇到了使用Numberformatinfo的地方,我不得不同时为一个国家写两个货币符号。

台湾现在使用 TWD 作为货币符号和 。所以他们将货币写为 NTD 23,900 起

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

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

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

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

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

似乎不​​支持两种货币 symbols/shortcuts 和/或四种预定义格式之一 (参见:remarks in documentation

简单版可以这样

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