Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Currency code to currency symbol mapping

Tags:

java

currency

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.

like image 738
artjomka Avatar asked Oct 08 '10 08:10

artjomka


People also ask

How do you make the 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.

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.

How do I get the currency symbol from locale?

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.


Video Answer


2 Answers

@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());
like image 144
Kevin D Avatar answered Oct 08 '22 15:10

Kevin D


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.

like image 34
Nivas Avatar answered Oct 08 '22 13:10

Nivas