Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

java - Acquiring a country's currency code

I have a problem getting a country's currency code. My task is to get the user's location, find out what country he is right now and get this country's currency code. Here's the code that fetches the country name and country code from the acquired location:

Geocoder gc = new Geocoder(this);
List<Address> addresses = gc.getFromLocation(
                location.getLatitude(), location.getLongitude(), 5);

textView1.setText(addresses.get(0).getCountryName());
textView2.setText(addresses.get(0).getCountryCode());

This works perfectly fine. Now I should use the java.util.Currency class to get a Currency object. I can use the Currency.getInstance(Locale locale) method. But there's no constructor in the Locale class that allows only the country code to be passed as an argument. Means I am not able to create a Locale object for the country. How can this be solved? Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should be able to use Currency.getInstance(new Locale("",code)), with a possible Exception if the country code isn't valid.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...