We maintain a master data table in SQL that contains the currency code, description, and symbol of all currencies our company does business in. In total there are 32. Example;
USD, US Dollar, $
These codes are used to look up exchange rate information which is automatically inserted into another SQL table. All of our .NET applications support these currencies. A user sets their preferred currency in their profile.
We just recently starting using Telerik controls, and the RadNumericTextBox control takes a culture string as a property (i.e en-US) which handles all culture specific settings (symbol, separator, grouping, etc) which makes monetary input quite nice for a user, but this creates an issue.
Now to the actual issue. I bind a drop down list with a list of objects, and in order to have all required information for the telerik controls and exchange rate information the underlying values need to be in this format. Example;
: --> en-US:USD
Logic that builds the currency objects
public static List<Currency> BuildCurrencyObjects()
{
List<Currency> currencies = new List<Currency>();
List<Currency> dbCurrencies = CurrencyMgmt.GetCurrencies();
foreach (Currency currency in dbCurrencies)
{
var region = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(c => new RegionInfo(c.LCID))
.Where(ri => ri != null && ri.ISOCurrencySymbol == currency.CurrenyCode)
.FirstOrDefault();
if (region != null)
{
var culture = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Where(x => x.Name.EndsWith((region as RegionInfo).Name))
.FirstOrDefault();
if (culture != null)
{
Currency c = new Currency()
{
CultureCode = culture.Name + ":" + region.ISOCurrencySymbol,
Symbol = culture.NumberFormat.CurrencySymbol,
Description = region.CurrencyEnglishName,
CurrencyRegion = region.EnglishName,
CurrenyCode = region.Name
};
currencies.Add(c);
}
}
}
return currencies;
}
I wrote this function pretty quickly and it appeared to work correctly. It creates 31 currency objects and displayed what appeared to be correct text in the drop down. I only realized there was an issue when it was moved to our test environment where the United States display text went from; and the value;
United States, US Dollar, $ --> Carribean, US Dollar, $ en-US:USD --> en-029:USD
I understand now that the above logic wont work since there are multiple cultures that map back to a currency code and Linq is returning the first element in the sequence. Is there any proficient way to map a currency code back to a culture string?
Short answer: Is not possible. Look what happen with the EURO. You could see a more examples here
Your solution should include a way to store the actual culture along with the currency.