tags:

views:

59

answers:

3

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.

+2  A: 

You can use the Currency object's getSymbol method.

What symbol is used depends on the Locale which is used See this and this.

Nivas
I tried to use getSymbol for USD then return is dollar sign (correct) but if I use EUR or DKK as currency code then the result is EUR or DKK http://ideone.com/ehmlP
artjomka
What symbol is used depends on the Locale which is used. See articles in my (updated) answer
Nivas
As I understood if I want to show appropriate currency symbol I need to keep Locale to format value in type I need am I right (For example to correctly show GBP currency symbol I need to have GBP currency mapped for example on Locale.UK) ?
artjomka
A: 

You can use Currency class and DecimalFormat class for achieve your requirement. In following example, # represents number and ¤ represents currency symbol, you can find relevant format parameters in java API doc for DecimalFormat class.

        Currency currency = Currency.getInstance("USD");

        DecimalFormat decimalFormat = new DecimalFormat("#¤");
        decimalFormat.setCurrency(currency);
        System.out.println(decimalFormat.format(234));
Manjula
As Nivas said, you can directly get relevant symbol using getSymbol() method of Currency class and can append it separately to the number too.
Manjula
A: 

@artjomka

I was able to replicate 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 gethe the sympbol for the Euro.

Locale.setDefault(Locale.UK);
Currency c  = Currency.getInstance("EUR");
System.out.println(c.getSymbol());
Kevin D
Nivas edited his answer whilst I writing mine. His linked articles should see you right.
Kevin D