我希望按货币名称获取货币符号(例如 $ 或£)(例如 USD 或 EUR )。
对于英语(美国)我可以获得符号(如果英语(美国)在设备上设置为语言):
Currency currency = Currency.getInstance(Locale.getDefault());
String symbol = currency.getSymbol()); // $
如何使用android工具按货币名称获取货币符号 -
someMethod (String currCode) { // currCode - USD, EUR, UAH
return currSymbol; // $...
}
答案 0 :(得分:26)
你可以试试这样的:
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));
}
}
}
答案 1 :(得分:16)
货币符号取决于位置。同一美元符号表示美国和澳大利亚的货币不同。因此,要获取正确的符号,您必须提供Locale实例。否则,将应用默认语言环境,这将导致不同设备的值不同。
Locale uk = new Locale("en", "GB");
Currency pound = Currency.getInstance("GBP");
pound.getSymbol(uk);
答案 2 :(得分:5)
我相信java.util.currency是你需要的类,调用getSymbol从货币名称获取符号
http://developer.android.com/reference/java/util/Currency.html
答案 3 :(得分:5)
我已经解决了这个问题,创建了一个Map,其中键是货币代码,值是符号。
public final Map<String, String> CURRENCIES= new HashMap<String, String>(){
{
put("EUR","€");
put("USD","$");
...
}
};
然后,您可以使用Locale获取符号,如下所示。
Currency currency = Currency.getInstance(Locale.getDefault());
String symbol = CURRENCIES.get(currency.getCurrencyCode());
答案 4 :(得分:1)
这对我有用。
JSONField
答案 5 :(得分:1)
您可以简单地使用此代码来获取货币符号和不同的货币格式。这里变量“curr”将从用户那里获取输入值。然后将其转换为不同的货币格式及其符号。在本文中,我解释了 4 种货币格式。通过这种方式,您也可以使用其他国家/地区的格式。
import java.util.*;
import java.text.*;
public class CurrencyConvertor{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double curr = scanner.nextDouble();
scanner.close();
if(curr>=0 && curr<=1000000000){
NumberFormat france = NumberFormat.getCurrencyInstance(Locale.FRANCE);
NumberFormat us = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat china = NumberFormat.getCurrencyInstance(Locale.CHINA);
NumberFormat india = NumberFormat.getCurrencyInstance(Locale.ENGLISH);
System.out.println("France: " + france.format(curr));
System.out.println("US: " + us.format(curr));
System.out.println("India: "+NumberFormat.getCurrencyInstance(new Locale("en","IN")).format(curr));
System.out.println("China: "+ china.format(curr));
}
}
}
答案 6 :(得分:0)
这是一个简单的解决方案。如上所述:
Currency currency = Currency.getInstance("INR");
String symbol = currency.getSymbol();