java 如何使用语言环境获取特定国家/地区的货币符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29969439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the currency symbol for particular country using locale?
提问by silverFoxA
I have tried this code and it gives me the Country Code
for some countries instead of the currency symbol
我试过这个代码,它给了我Country Code
一些国家的而不是currency symbol
I want the currency symbol not the code
我想要货币符号而不是代码
The array resourseList contains all the countries with it's code
数组 resourseList 包含所有国家及其代码
String m= (String) Array.get(recourseList,i);
String[] ma=m.split(",");
Locale locale=new Locale("en", ma[1]);
Currency currency= Currency.getInstance(locale);
String symbol = currency.getSymbol();
( (TextView) finalV.findViewById(R.id.currencySymbolid)).setText(symbol);
回答by MaxZoom
It says in the Currencyspecification:
它在货币规范中说:
getSymbol() gets the symbol of this currency for the default locale. For example, for the US Dollar, the symbol is "$" if the default locale is the US, while for other locales it may be "US$". If no symbol can be determined, the ISO 4217 currency code is returned.
getSymbol() 获取默认语言环境下该货币的符号。例如,对于美元,如果默认语言环境是美国,则符号是“$”,而对于其他语言环境,它可能是“US$”。如果无法确定符号,则返回 ISO 4217 货币代码。
EDITEDI have found a way around this issue
编辑我找到了解决这个问题的方法
import java.util.Currency;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class CurrencyCode
{
public static void main() {
Map<Currency, Locale> map = getCurrencyLocaleMap();
String [] countries = { "US", "CA", "MX", "GB", "DE", "PL", "RU", "JP", "CN" };
for (String countryCode : countries) {
Locale locale = new Locale("EN",countryCode);
Currency currency = Currency.getInstance(locale);
String symbol = currency.getSymbol(map.get(currency));
System.out.println("For country " + countryCode + ", currency symbol is " + symbol);
}
}
public static Map<Currency, Locale> getCurrencyLocaleMap() {
Map<Currency, Locale> map = new HashMap<>();
for (Locale locale : Locale.getAvailableLocales()) {
try {
Currency currency = Currency.getInstance(locale);
map.put(currency, locale);
}
catch (Exception e){
// skip strange locale
}
}
return map;
}
}
This prints:
这打印:
For country US, currency symbol is $
For country CA, currency symbol is $
For country MX, currency symbol is $
For country GB, currency symbol is £
For country DE, currency symbol is
For country PL, currency symbol is z?
For country RU, currency symbol is руб.
For country SE, currency symbol is kr
For country JP, currency symbol is ¥
For country CN, currency symbol is ¥