gpt4 book ai didi

android - 如何通过货币名称获取货币符号?

转载 作者:IT老高 更新时间:2023-10-28 21:46:22 25 4
gpt4 key购买 nike

我想通过货币名称(如 USDEUR 获取货币符号(如 $£) )。

对于英语(美国),我可以获得符号(如果英语(美国)设置为设备上的语言):

Currency currency = Currency.getInstance(Locale.getDefault());
String symbol = currency.getSymbol()); // $

如何使用 android 工具按货币名称获取货币符号 -

someMethod (String currCode) { // currCode - USD, EUR, UAH
return currSymbol; // $...
}

最佳答案

你可以试试这样的:

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

//for your case that you want to get Euro symbol because France are with Euro symnol
Utils.getCurrencySymbol(Currency.getInstance(Locale.FRANCE).getCurrencyCode());
//you can get symbol also if you write string of your desired currency
Utils.getCurrencySymbol("INR");


}


static 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));
}

}
}

关于android - 如何通过货币名称获取货币符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36258511/

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