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?
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.
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.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With