• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Country to Currency Mapping

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will this work?

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Currency.html
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Rob
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic