Android_Superman Android_Superman - 29 days ago 17
Android Question

How to convert country name into currency code?

Suppose I have country name as USA .So I want to convert to USD.I have only country name from webservice.

I have tried this code but how to convert country name into Locale object. Can anybody suggest in this problem?

String countryName=jsonObject.getString("country");

Answer

You can also use https://github.com/TakahikoKawasaki/nv-i18n library this is specifically designed for this purpose.

From its GitHub page,

Package to support internationalization, containing ISO 3166-1 country code enum, ISO 639-1 language code enum, etc.

Example specific to this question

String countryName = jsonObject.getString("country");
List<CurrencyCode> codes = CurrencyCode.getByCountry(countryName, false)

Simple example to see available list (from Github page),

// List all the currency codes.
for (CurrencyCode code : CurrencyCode.values())
{
    System.out.format("[%s] %03d %s\n", code, code.getNumeric(), code.getName());
}

// List all the country codes.
for (CountryCode code : CountryCode.values())
{
    System.out.format("[%s] %s\n", code, code.getName());
}