How to handle countries that use multiple currencies in .NET?

I have an application in which I want to format the currency using the formatting of the country's national currency. The problem is that some countries use multiple currencies, but in .NET one currency is assigned for each country. For example, Romania uses EUR and RON . When I get currency information from .NET:

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

The output of leu , which is a type of RON currency.

How do I get EUR for this case in .NET? I have a three-digit ISO currency code ( EUR ) and the country language ( ro-RO ), but I don’t know how to use this information to enter the correct formatted Euro currency string.

+7
source share
2 answers

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

 NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone(); LocalFormat.CurrencySymbol = "€"; decimal money = 100; Console.WriteLine(money.ToString("c", LocalFormat)); 
+1
source

I thought I would give you the answer of a static helper class, for example:

 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 any variable that you want to use in this method and perform any operations that you want. Call the following:

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

The problem is that you should call this helper when you want to get currency information instead of cultureInfo.NumberFormat.CurrencySymbol

0
source

All Articles