問題描述
我在獲取系統(tǒng)的默認(rèn)貨幣符號(hào)時(shí)遇到了一些問題.我以這種方式獲取貨幣符號(hào):
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());
當(dāng)系統(tǒng)語言為英語(美國(guó))
時(shí),右側(cè)的符號(hào)會(huì)顯示($
).但是當(dāng)我選擇語言 Portuguese (Portugal)
它返回這個(gè)符號(hào) ¤
.
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 ¤
.
這是什么原因造成的?
推薦答案
這似乎是一個(gè)已知問題(http://code.google.com/p/android/issues/detail?id=38622.我通過這種方式得出了一個(gè)可能的解決方案:
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:
由于問題出在符號(hào)而不是貨幣代碼中,我解決了這個(gè)問題,創(chuàng)建了一個(gè)靜態(tài)Map
,其中鍵是 CurrencyCode,值是 符號(hào).
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","$");
(..)
}
};
為了獲取語言環(huán)境信息中可用的所有(或幾乎)貨幣代碼,您可以執(zhí)行以下操作:
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 創(chuàng)建地圖后,您只需要這樣:
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());
這篇關(guān)于語言環(huán)境貨幣符號(hào)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!