Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Country to Currency Mapping

 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given a ISO 3166-1 country code, what is the best possible way to get the corresponding ISO 4217 currency code in Java? For example, for the country code 'US' I should get the corresponding currency code 'USD'.

Is there any library or utility class providing such facility in Java? I had a look at the Currency class in java but, that gives the currency code for a given locale. Thanks!
 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinking about it, I found a solution and is given below:



In this code, I am constructing a new Locale with the correct ISO country code for France(FR) and the wrong language(en). I am pssing this Locale instance to the Currency class and thereby, obtaining the Currency Code for the given country.

Is there any problem by constructing a Locale instance by passing a wrong langauge? Thanks!
 
Rajesh Narkhed
Greenhorn
Posts: 6
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajesh Narkhed:
will this work?

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Currency.html

Apparently it will, since half an hour before your post Jacob has posted a working solution that uses Currency.

Originally posted by Jacob Thomas:
Is there any problem by constructing a Locale instance by passing a wrong langauge? Thanks!

If you are using it for only this, then no. Currency.getInstance(Locale) only uses the Locale's country.

I wouldn't use the Locale for other means though, since I expect you to run into unexpected results then.
[ October 02, 2008: Message edited by: Rob Prime ]
 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Rob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic