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

2

8

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.

Ingot answered 20/6, 2012 at 0:18 Comment(3)
Romania isn't among countries using EURO yet. .NET is displaying the currency correctly. Don't confuse EU member states with EU states using EURO currency, it's not the same. Those lists aren't 100% equal, because many countries still don't use EURO (Great Britain for instance).Stalinsk
True, but we support EUR payments in Romania and the UK, for example. Though they may not be 'standardized' by the country, in practice they are used and therefore we must support them.Ingot
I'm guessing that if it's not 'standardized' then .net has no way of knowing about it. (That is unless you code it explicitly)Myrnamyrobalan
S
1

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));
Scottiescottish answered 20/6, 2012 at 0:31 Comment(1)
Is this really the best way to handle this?Pavement
T
0

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

Tiki answered 20/6, 2012 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.