有没有办法将ISO货币代码映射回文化字符串?

时间:2015-08-06 01:52:59

标签: c# asp.net .net linq telerik

我们在SQL中维护一个主数据表,其中包含我们公司开展业务的所有货币的货币代码,描述和符号。总共有32.示例;

美元,美元,美元

这些代码用于查找自动插入另一个SQL表的汇率信息。我们所有的.NET应用程序都支持这些货币。用户在其个人资料中设置其首选货币。

我们最近刚开始使用Telerik控件,而RadNumericTextBox控件将文化字符串作为属性(即en-US)处理所有特定于文化的设置(符号,分隔符,分组等),这使得货币输入非常适合用户,但这会产生问题。

现在到实际问题。我将下拉列表与对象列表绑定,并且为了获得telerik控件和汇率信息的所有必需信息,基础值必须采用此格式。实施例;

: - >的en-US:USD

构建货币对象的逻辑

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

我很快写了这个函数,它似乎工作正常。它创建31个货币对象,并在下拉列表中显示看似正确的文本。我只是意识到当它被转移到我们的测试环境时会出现问题,美国的显示文字来自于此;和价值;

美国,美元,$ - &gt;加勒比海,美元,$ en-US:USD - &gt;烯029:USD

我现在明白上述逻辑不会起作用,因为有多种文化可以映射回货币代码而Linq正在返回序列中的第一个元素。是否有任何熟练的方法将货币代码映射回文化字符串?

3 个答案:

答案 0 :(得分:0)

根据我的理解,有许多文化使用单一货币代码。这是因为并非所有国家都有自己的货币。这意味着无法对货币进行映射以将货币映射到特定文化,而无需对值进行硬编码或将其添加到数据库中。您最好的选择是在数据库中添加另一列,以定义该货币所属的文化。

答案 1 :(得分:0)

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.

答案 2 :(得分:0)

一个好主意是设置语言(例如 en)硬编码或隐式设置 CurrencyNegativePattern、CurrencyPositivePattern 和 CurrencyDecimalSeperator(如 How to display the euro symbol at the front of the number when using CultureInfo? 中建议的那样)。

相关问题