The example presented in this code snippet show you how to get the available currency codes. We will need the locale information and use the Currency
class for this example.
package org.kodejava.example.util;
import java.util.Currency;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
public class CurrencySymbolDemo {
public static void main(String[] args) {
CurrencySymbolDemo cs = new CurrencySymbolDemo();
Map<String, String> currencies = cs.getAvailableCurrencies();
for (String country : currencies.keySet()) {
String currencyCode = currencies.get(country);
System.out.println(country + " => " + currencyCode);
}
}
/**
* Get the currencies code from the available locales information.
*
* @return a map of currencies code.
*/
private Map<String, String> getAvailableCurrencies() {
Locale[] locales = Locale.getAvailableLocales();
// We use TreeMap so that the order of the data in the map sorted
// based on the country name.
Map<String, String> currencies = new TreeMap<>();
for (Locale locale : locales) {
try {
currencies.put(locale.getDisplayCountry(),
Currency.getInstance(locale).getCurrencyCode());
} catch (Exception e) {
// when the locale is not supported
}
}
return currencies;
}
}
You will have something like this printed on the screen:
...
Honduras => HNL
Hong Kong => HKD
Hungary => HUF
Iceland => ISK
India => INR
Indonesia => IDR
Iraq => IQD
Ireland => EUR
Israel => ILS
Italy => EUR
...
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020
This is not possible with API less than 19. What to do for Older versions?
What do you mean by API less than 19? Are you doing an Android project?