如何使用区域设置获取特定国家/地区的货币符号?
How to get the currency symbol for particular country using locale?
我试过这段代码,它给了我一些国家的 Country Code
而不是 currency symbol
I want the currency symbol not the code
数组资源列表包含所有国家代码
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);
Currency规范中说:
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.
已编辑
我找到了解决这个问题的方法
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;
}
}
这会打印:
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 ¥
我试过这段代码,它给了我一些国家的 Country Code
而不是 currency symbol
I want the currency symbol not the code
数组资源列表包含所有国家代码
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);
Currency规范中说:
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.
已编辑 我找到了解决这个问题的方法
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;
}
}
这会打印:
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 ¥