17

Is it possible to get the currency code of the country, where the user and device is. i want to set the country code of the present country of the user as default country. Do we have a solution for this in android ?

Vivek Barai
  • 1,338
  • 13
  • 26
Droid_Dev
  • 1,162
  • 1
  • 8
  • 25

2 Answers2

36

As this piece of code might be helpfull for you ,

public class CurrencyTest {
    public static void main(String[] args) throws Exception {
        Locale defaultLocale = Locale.getDefault();
        displayCurrencyInfoForLocale(defaultLocale);

        Locale swedishLocale = new Locale("sv", "SE");
        displayCurrencyInfoForLocale(swedishLocale);
    }

    public static void displayCurrencyInfoForLocale(Locale locale) {
        System.out.println("Locale: " + locale.getDisplayName());
        Currency currency = Currency.getInstance(locale);
        System.out.println("Currency Code: " + currency.getCurrencyCode());
        System.out.println("Symbol: " + currency.getSymbol());
        System.out.println("Default Fraction Digits: " + currency.getDefaultFractionDigits());
        System.out.println();
    }
}
activesince93
  • 1,716
  • 17
  • 37
Fatti Khan
  • 1,543
  • 1
  • 11
  • 19
  • If locale throws an exception, then try out this solution https://stackoverflow.com/a/71768216/14594657 – Aknk Nov 24 '22 at 12:14
8

get locale of device and then check http://www.avajava.com/tutorials/lessons/how-do-i-display-the-currency-for-a-locale.html

Locale current = getResources().getConfiguration().locale;
Log.i("locale", Currency.getInstance(current).getCurrencyCode());
MohK
  • 1,873
  • 1
  • 18
  • 29