gpt4 book ai didi

java - 如果我们在数据库中只有国家代码,则获取货币代码

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:32 26 4
gpt4 key购买 nike

我在获取国家/地区货币时遇到问题,我通过这些代码在数据库中获取了国家/地区代码,我必须在 Java 中获取国家/地区代码如何为此编写代码请给我建议。我正在尝试使用此示例,但它不起作用。

    class Utils {
public static SortedMap<Currency, Locale> currencyLocaleMap;
static {
currencyLocaleMap = new TreeMap<Currency, Locale>(
new Comparator<Currency>() {
public int compare(Currency c1, Currency c2) {
return c1.getCurrencyCode().compareTo(
c2.getCurrencyCode());
}
});
for (Locale locale : Locale.getAvailableLocales()) {
try {
Currency currency = Currency.getInstance(locale);
currencyLocaleMap.put(currency, locale);
} catch (Exception e) {
}
}
}

public static String getCurrencySymbol(String currencyCode) {
Currency currency = Currency.getInstance(currencyCode);
System.out.println(currencyCode + ":-"
+ currency.getSymbol(currencyLocaleMap.get(currency)));
return currency.getSymbol(currencyLocaleMap.get(currency));
}
}

public class GetCurrency {
public static void main(String[] args) {

Utils.getCurrencySymbol(Currency.getInstance("INR").getCurrencyCode());
Utils.getCurrencySymbol(Currency.getInstance(Locale.JAPAN)
.getCurrencyCode());
Utils.getCurrencySymbol(Currency.getInstance(Locale.UK)
.getCurrencyCode());

Utils.getCurrencySymbol("INR");
}
}

最佳答案

尝试:

//to retrieve currency code
public static String getCurrencyCode(String countryCode) {
return Currency.getInstance(new Locale("", countryCode)).getCurrencyCode();
}

//to retrieve currency symbol
public static String getCurrencySymbol(String countryCode) {
return Currency.getInstance(new Locale("", countryCode)).getSymbol();
}

关于java - 如果我们在数据库中只有国家代码,则获取货币代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16832655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com