Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert iso 4217 numeric currency code to currency name

Tags:

java

iso

applepay

I have a an ISO 4217 numeric currency code: 840

I want to get the currency name: USD

I am trying to do this:

 Currency curr1 = Currency.getInstance("840");

But I keep getting

java.lang.IllegalArgumentException

how to fix? any ideas?

like image 988
Asaf Maoz Avatar asked Nov 04 '14 11:11

Asaf Maoz


People also ask

How do I use ISO currency code?

The currency pair indicates how much of the quote currency is needed to purchase one unit of the base currency. For example, EUR/USD is the quote for the euro against the U.S. dollar. EUR is the three-letter ISO currency code for the euro, and USD is the code for the U.S. dollar.

What is a 3 letter currency code?

The first two letters of the ISO 4217 three-letter code are the same as the code for the country name, and, where possible, the third letter corresponds to the first letter of the currency name. For example: The US dollar is represented as USD – the US coming from the ISO 3166 country code and the D for dollar.


1 Answers

java.util.Currency.getInstance supports only ISO 4217 currency codes, not currency numbers. However, you can retrieve all currencies using the getAvailableCurrencies method, and then search for the one with code 840 by comparing the result of the getNumericCode method.

Like this:

public static Currency getCurrencyInstance(int numericCode) {
    Set<Currency> currencies = Currency.getAvailableCurrencies();
    for (Currency currency : currencies) {
        if (currency.getNumericCode() == numericCode) {
            return currency;
        }
    }
    throw new IllegalArgumentException("Currency with numeric code "  + numericCode + " not found");
}
like image 154
Erwin Bolwidt Avatar answered Oct 04 '22 01:10

Erwin Bolwidt