语言环境货币符号

我在获取系统的默认货币符号时遇到了一些问题.我以这种方式获取货币符号:

I having some problems getting the default currency symbol of the system. I am getting the currency symbol this way:

Currency currency = Currency.getInstance(Locale.getDefault());
Log.v("TAG",currency.getSymbol());

当系统语言为英语(美国)时,右侧的符号会显示($).但是当我选择语言 Portuguese (Portugal) 它返回这个符号 ¤.

When the system language is in English (United States) the right symbol shows up ($). But when i choose the language Portuguese (Portugal) it returns this symbol ¤.

这是什么原因造成的?

推荐答案

这似乎是一个已知问题(http://code.google.com/p/android/issues/detail?id=38622.我通过这种方式得出了一个可能的解决方案:

This seems to be a known issue (http://code.google.com/p/android/issues/detail?id=38622. I came to a possible solution this way:

由于问题出在符号而不是货币代码中,我解决了这个问题,创建了一个静态Map,其中键是 CurrencyCode,值是 符号.

Since the problem is in the Symbol and not the Currency code, i solved this problem creating a staticMap where the key is the CurrencyCode and the value is the Symbol.

public static final Map<String, String> MYCURRENCIES = new HashMap<String, String>(){
        {
            put("EUR","€");
            put("USD","$");
            (..)
        }
};

为了获取语言环境信息中可用的所有(或几乎)货币代码,您可以执行以下操作:

In order to get all (or almost) the currencies codes available in the locales information you can do something like this:

for (Locale ll: Locale.getAvailableLocales()){
    try {
       Currency a = Currency.getInstance(ll);
       Log.v("MyCurrency",a.getCurrencyCode()+"#"+a.getSymbol());
    }catch (Exception e){
       // when the locale is not supported
  }
}

使用 CurrencyCode 和 Symbol 创建地图后,您只需要这样:

After you created you Map with the CurrencyCode and Symbol you just have to something like this:

Currency currency = Currency.getInstance(Locale.getDefault());
String curSymbol = MYCURRENCIES.get(currency.getCurrencyCode());

相关文章