0

While writing a piece of code, I came across where using Numberformatinfo, I had to write two currency symbols for a country at the same time.

Taiwan, now uses TWD as their currency symbol along with . So they write their Currency as NTD 23,900 起.

But just by using NumberformatInfo, I am not able to put two currency symbols at the same time.

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

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

Here I can change the symbol, but to only one of the above mentioned, which can be placed either before the amount or after.

jazb
  • 5,498
  • 6
  • 37
  • 44
  • There ssems to be even more problems with culture info. See this post *(including comments and answers)*: https://stackoverflow.com/questions/49926486/what-controls-the-currencypositivepattern-in-net – Julo Nov 28 '18 at 05:45

1 Answers1

1

I'm afraid, there is only one way, how to do this. You need to implement a custom type with a custom formatter.

There seems not be a support for two currency symbols/shortcuts and or one of four predefined formats (see: remarks in documentation)

Simple version can be like this.

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

The example is constructed for EURO currency (my locale). In your real implementation you need to determine, whether the format should be changed, e.g. if if ((format == "C") && IsTaiwan(formatProvider)).

Julo
  • 1,102
  • 1
  • 11
  • 19
  • I'm glad my answer helped you. Unfortunately I had no time to make a better example. I'm afraid that the country detection is not that simple from format provider only. And there is also a problem with specific formats like `C2`. – Julo Dec 05 '18 at 16:27