21

Below program prints the Currency symbol given the ISO 4217 currency code.

import java.util.*;

public class Currency{

    public static void main(String args[]) {
        Currency myInstance = Currency.getInstance(args[0]);
        System.out.println(myInstance.getSymbol());
    }
}

Problem: Works fine when the string USD is input. For other inputs like EUR just return the Currency Code.

Sample input , ouput from the program:

input: java Currency USD 
output: $
input: java Currency EUR
output: EUR -> I expect the symbol of Euro here
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
Eternal Learner
  • 3,800
  • 13
  • 48
  • 78

4 Answers4

31

Currency.getSymbol() returns the currency symbol relative to the default locale.

Gets the symbol of this currency for the default 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.

Use Currency.getSymbol(Locale locale) if you want the symbol for a different locale.

System.out.println(
   Currency.getInstance("USD").getSymbol(Locale.US)
);
// prints $

System.out.println(
   Currency.getInstance("USD").getSymbol(Locale.FRANCE)
);
// prints USD

System.out.println(
   Currency.getInstance("EUR").getSymbol(Locale.US)
);
// prints EUR

System.out.println(
   Currency.getInstance("EUR").getSymbol(Locale.FRANCE)
);
// prints €

(see also on ideone.com).

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • 1
    @polygenelubricats: The above works when we also know the locale country. I currently only have the ISO 4217 currency cod and nothing else. I need to figure out the locale from the currency code and then the implementation would be similar to above code. – Eternal Learner Oct 22 '10 at 05:27
  • @polygenelubricats: Yeah very similar , but instead of printing Locale , I just need the symbol of currency in that local. But this approach helps thanks. – Eternal Learner Oct 22 '10 at 05:48
  • The problem with the default Java implementation is that it's just dumb. For example, if I'm displaying euros in the US locale, I should see something like €75, not EUR75 - everyone in the US knows what the actual euro symbol is much better than "EUR". Thus, I always have to use custom code, like setting DecimalFormatSymbols manually, to get decent looking output instead of being able to depend on the stock implementation. – adevine Apr 21 '13 at 00:09
2

For me, your code even in the first case returns USD. It seemes, that Currency heavily depends on the JRE version (1.6 for me). Perosnally I recommend you to write your own CUR to symbol conversion module - it will be much easier, than try to use this one.

Alex Abdugafarov
  • 6,112
  • 7
  • 35
  • 59
1

If someone needs it the other way round (for example € -> EUR)

String currency = €;
String currencyCode = "";
for (Currency c : Currency.getAvailableCurrencies()) {
    if (c.getSymbol().equals(currency)) {
        currencyCode = c.toString();
    }
}
teh.fonsi
  • 3,040
  • 2
  • 27
  • 22
-1

Using the limited Locale enum's only caters for westernised symbols. If you want to be a little more global try using Locales provided by:

Locale[] locales = Locale.getAvailableLocales();

Using Locales from this list gave symbols rather than TLA's pretty consistently.

s.k
  • 519
  • 4
  • 7
  • 23