問題描述
我在獲取系統的默認貨幣符號時遇到了一些問題.我以這種方式獲取貨幣符號:
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());
這篇關于語言環境貨幣符號的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!