3
Currency currency = Currency.getInstance(currencyCode);

How do I get the symbol of the currency as it would appear in one of its native locales as opposed to the default locale?

currency.getSymbol() won't work because that will be based of the default locale. currency.getSymbol(Locale locale) won't work because the code will not be able to derive a proper locale based purely on the currencyCode.

Gabriel
  • 1,679
  • 3
  • 16
  • 37
  • A (possibly unhelpful!) response is that you should never need to do this. You're displaying currency symbols? Display them in the user's current locale. It never makes sense to display multiple currencies each in the locale they are used in. Consider how many countries use a dollar of some sort; each of them would simply be rendered as $. Hopeless! – Tom Anderson Feb 08 '12 at 00:07
  • This will be the path I end up taking. – Gabriel Feb 08 '12 at 20:26
  • 1
    I also want to do this. I don't care about locale. I only care about the symbol. I don't care if the symbol is the same i.e. "$" for multiple currencies as in my use case, it doesn't matter. – Hendy Irawan May 09 '14 at 15:43
  • 1
    There is another question: http://stackoverflow.com/questions/758255/java-currency-to-locale-mapping-possible which still doesn't give the pragmatic answer :( – Hendy Irawan May 09 '14 at 15:43

1 Answers1

2

While I agree with you when you said "the code will not be able to derive a proper locale based purely on the currencyCode", Currency.getInstance() also accepts a Locale as a parameter.

I think that this is going to be your best bet. Without more to go on, I'm not sure how you will derive the Locale from anything in your code, but presumably, if you can find a way, you can create a Locale object, and use it to grab an instance of a Currency object. It should be smooth sailing from there.

Good Luck!

References:

http://docs.oracle.com/javase/7/docs/api/java/util/Currency.html

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Locale.html

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Cody S
  • 4,744
  • 8
  • 33
  • 64
  • 1
    additionally, you can set your locale using something like Locale.setDefault(Locale.UK); – northpole Feb 07 '12 at 23:52
  • Your solution works when the locale is known. The question is about when the currency code is known but the locale is unknown. – Gabriel Feb 08 '12 at 20:29