Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

android - How to get currency symbol by currency name?

I want get Currency symbol (like $ or ) by currency name (like USD or EUR).

For English(US) I can get symbol (if English(US) set as language on device):

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

How can I get symbol for currency by currency name using android tools -

someMethod (String currCode) { // currCode - USD, EUR, UAH
    return currSymbol;       // $...
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can try some like this:

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

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...