从设备Android中获取货币



我如何从Android设备获得货币。

ex:我设置了英语,然后我可以获得美元货币。但是我住在新加坡,设备如何返回SGD?(新加坡不使用美元)。

ex:三星Galaxy J5使用英语我住在美国 ->美元。
我的朋友住在新加坡 ->新加坡美元

使用此方法来获取货币符号,在 common.java class> class

中写下此方法
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));
}

现在以这种方式获取货币符号。

    String currencySymbol =Common.getCurrencySymbol(Currency.getInstance(Locale.US).getCurrencyCode());
    String currencySymbol =Common.getCurrencySymbol(Currency.getInstance(Locale.JAPAN).getCurrencyCode());
    String currencySymbol =Common.getCurrencySymbol(Currency.getInstance(Locale.UK).getCurrencyCode());
    //for your case that you want to get Euro symbol because France are with Euro symnol    
    String currencySymbol =Common.getCurrencySymbol(Currency.getInstance(Locale.FRANCE).getCurrencyCode());
    //you can get symbol also if you write string of your desired currency
    String currencySymbol =Common.getCurrencySymbol("INR");
    String currencySymbol = CommonClass.getCurrencySymbol("ZAR");

希望这个帮助。

相关内容

  • 没有找到相关文章

最新更新