它sa ys在Currency規範中:
getSymbol()獲取此貨幣的默認語言環境的符號。 例如,對於美元,如果默認 區域設置爲美國,則符號爲「$」,而對於其他區域設置,則可以是「美元」。如果沒有確定 符號,則返回ISO 4217貨幣代碼。
EDITED 我已經找到一種方法解決此問題
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 ¥
你想貨幣代碼或貨幣符號? – MaxZoom
我在尋找符號 – silverFoxA
雖然您正在尋找符號,但請注意,有時貨幣代碼同樣重要。你可能想包括兩個。例如,美國和加拿大都使用$,但如果沒有代碼,則無法確定是美元還是加拿大元。 – JNYRanger