my dog learned polymorphism*
The moose likes Java in General and the fly likes Country to Currency Mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Country to Currency Mapping" Watch "Country to Currency Mapping" New topic
Author

Country to Currency Mapping

Jacob Thomas
Ranch Hand

Joined: May 06, 2003
Posts: 51
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

Joined: May 06, 2003
Posts: 51
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

Joined: Jan 03, 2008
Posts: 6
will this work?

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

Joined: Oct 27, 2005
Posts: 19656
    
  18

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jacob Thomas
Ranch Hand

Joined: May 06, 2003
Posts: 51
Thanks for the reply Rob
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Country to Currency Mapping
 
Similar Threads
Problems with Spring's default PropertyEditor search
JSTL - Fmt parseNumber problem
UK Locale problem
how to get numeric value of a currency code?
Getting currency symbols