Good day, in database there is table with houses for sale records. For each house record there is currency code (in ISO 4217 format) field. Is it possibly to somehow get currency symbol from that code so I could use it on presentation side ?
Thank you.
P.S. Was trying to resolve that problem setting Currency object (created by Currency.getInstance(currencyCode)) into DecimalNumberFormat setCurrency method and then format value I needed to display, but formatted value still without currency symbol.
public final Map<String, String> CURRENCIES= new HashMap<String, String>(){ { put("EUR","€"); put("USD","$"); ... } }; Then, you can get the symbol by using Locale, like this. Show activity on this post. You can simply use this code to get the currency symbol and different currency formats.
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.
The getSymbol() method is used to get the symbol of a given currency for the default DISPLAY locale. For example, for the US Dollar, the symbol is "$" if the default locale is the US, while for other locales it may be "US$". If no symbol can be determined, the ISO 4217 currency code is returned.
@artjomka
I was able to reproduce your problem by setting my default locale to Latvia
Locale.setDefault(new Locale("lv","LV"));
Currency c = Currency.getInstance("EUR");
System.out.println(c.getSymbol());
This gave me the output of "EUR".
However, by leaving setting my locale to Uk (already my default) I get the symbol for the Euro(€).
Locale.setDefault(Locale.UK);
Currency c = Currency.getInstance("EUR");
System.out.println(c.getSymbol());
You can use the Currency
object's getSymbol method.
What symbol is used depends on the Locale which is used See this and this.
Update, Jan 2016: The links are now dead. But they were specific to Java 1.4/5 so not really relevant anymore. More details on currency formatting can be found in https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html. The links can be found on the WayBackEngine though.
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