Daniel T.
Daniel T.

Reputation: 38410

How do I handle countries that use multiple currencies in .NET?

I have an application where I want to format a currency using the country's native currency formatting. Problem is, certain countries use multiple currencies, but .NET only assigns one currency per country. For example, Romania uses EUR and RON. When I get the currency info from .NET:

var cultureInfo = new CultureInfo("ro-RO");
Console.WriteLine("cultureInfo.NumberFormat.CurrencySymbol);

The output is leu, which is the RON currency type.

How would I get EUR for this case in .NET? I have the 3-letter ISO currency code (EUR) and the country language (ro-RO) but I don't know how to use this info to get a correctly-formatted euros currency string.

Upvotes: 8

Views: 2781

Answers (2)

astro boy
astro boy

Reputation: 1430

I thought I'd give you a static helper class answer like following:

static class CurrencySymbolHelper
{
    public static string GetCurrencySymbol(CultureInfo cultureInfo, bool getAlternate)
    {
        if (cultureInfo.Name == "ro-RO" && getAlternate)
                return "EUR";

        return cultureInfo.NumberFormat.CurrencySymbol;
    }
}

You can pass what ever variable you want into the method and do any operations within it you wish. Call as following:

var cultureInfo = new CultureInfo("ro-RO");
Console.WriteLine(CurrencySymbolHelper.GetCurrencySymbol(cultureInfo,false));

Issue is, you have to call this helper when ever you want to get currency info instead of cultureInfo.NumberFormat.CurrencySymbol

Upvotes: 0

fenix2222
fenix2222

Reputation: 4730

You can replace currency symbol with a custom one (leu to euro in this case)

NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
LocalFormat.CurrencySymbol = "€";

decimal money = 100;
Console.WriteLine(money.ToString("c", LocalFormat));

Upvotes: 1

Related Questions