How to get currency symbol by currency name?
Asked Answered
D

6

50

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;       // $...
}
Derringdo answered 28/3, 2016 at 8:16 Comment(1)
note that the currency symbol is not always put in before the money value in other languages, so it's better to format the currency valueRyanryann
L
36

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

    }
}
Levi answered 28/3, 2016 at 8:29 Comment(2)
@Shevchenko Arthem you can try my update maybe this is better solution for your case.Levi
Currency.getInstance(currency).getSymbol(Locale.getDefault())Bojorquez
H
24

Currency symbol depends on location. The same $ sign means different currencies in US and Australia. So, to get the correct symbol you have to provide the Locale instance. Otherwise, a default locale will be applied, which will result in different values for different devices.

    Locale uk = new Locale("en", "GB");
    Currency pound = Currency.getInstance("GBP");
    pound.getSymbol(uk);
Heyman answered 28/3, 2016 at 9:3 Comment(0)
C
17

This works for me.

Currency currency = Currency.getInstance("GBP");
String symbol = currency.getSymbol();
Chape answered 27/12, 2018 at 23:34 Comment(0)
L
8

I've solved this problem to create a Map which has the key is the currency code and the value is the symbol.

public final Map<String, String> CURRENCIES= new HashMap<String, String>(){
        {
            put("EUR","€");
            put("USD","$");
            ...
        }
}; 

Then, you can get the symbol by using Locale, like this.

Currency currency = Currency.getInstance(Locale.getDefault());
String symbol = CURRENCIES.get(currency.getCurrencyCode());
Legation answered 28/3, 2016 at 8:26 Comment(0)
O
7

I believe java.util.currency is the class you need, call getSymbol to get symbol from currency name

http://developer.android.com/reference/java/util/Currency.html

Odilia answered 28/3, 2016 at 8:20 Comment(0)
P
1

You can simply use this code to get the currency symbol and different currency formats. Here the variable "curr" will take the input value from user. And then convert it into different currency formats along with their symbols. In this I have explained 4 currency format. In this way you can use format for other countries also.

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






Pairs answered 12/7, 2021 at 9:48 Comment(2)
Perhap you can explain what the code doesFidellia
Now I have tried to explain a bit.Is this fine?Pairs

© 2022 - 2024 — McMap. All rights reserved.