7

I have tried this code and it gives me the Country Codefor some countries instead of the currency symbol

I want the currency symbol not the code

The array resourseList contains all the countries with it's code

String m= (String) Array.get(recourseList,i);
String[] ma=m.split(",");
Locale locale=new Locale("en", ma[1]);
Currency currency= Currency.getInstance(locale);
String symbol = currency.getSymbol();
((TextView) finalV.findViewById(R.id.currencySymbolid)).setText(symbol);
Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
silverFoxA
  • 4,549
  • 7
  • 33
  • 73

1 Answers1

16

It says in the Currency specification:

getSymbol() gets the symbol of this currency for the default locale. For example, for the US Dollar, the symbol is "$" if the default locale is the US, while for other locales it may be "US$". If no symbol can be determined, the ISO 4217 currency code is returned.

EDITED I have found a way around this issue

import java.util.Currency;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class CurrencyCode
{

    public static void main() {
        Map<Currency, Locale> map = getCurrencyLocaleMap();
        String [] countries = { "US", "CA", "MX", "GB", "DE", "PL", "RU", "JP", "CN" };

        for (String countryCode : countries) {
           Locale locale = new Locale("EN",countryCode);
           Currency currency = Currency.getInstance(locale);
           String symbol = currency.getSymbol(map.get(currency));
           System.out.println("For country " + countryCode + ", currency symbol is " + symbol);
        }
    }

    public static Map<Currency, Locale> getCurrencyLocaleMap() {
       Map<Currency, Locale> map = new HashMap<>();
        for (Locale locale : Locale.getAvailableLocales()) {
           try {
             Currency currency = Currency.getInstance(locale);
             map.put(currency, locale);
           }
           catch (Exception e){ 
             // skip strange locale 
           }
        }
        return map;
    }
}

This prints:

For country US, currency symbol is $
For country CA, currency symbol is $
For country MX, currency symbol is $
For country GB, currency symbol is £
For country DE, currency symbol is €
For country PL, currency symbol is zł
For country RU, currency symbol is руб.
For country SE, currency symbol is kr
For country JP, currency symbol is ¥
For country CN, currency symbol is ¥
MaxZoom
  • 7,619
  • 5
  • 28
  • 44
  • i understand but please check my code, I'm changing the local – silverFoxA Apr 30 '15 at 14:14
  • What Locale do you use? – MaxZoom Apr 30 '15 at 14:16
  • I have clearly mentioned in the question itself the `resourcelist` contains all the country code – silverFoxA Apr 30 '15 at 14:23
  • what is the country code?? the one that i have given? – silverFoxA Apr 30 '15 at 15:19
  • Those are the [ISO](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) country code – MaxZoom Apr 30 '15 at 15:20
  • Hmm I don't think this is a very good solution after playing around with it. Many different locales will use the same Currency, such as "sn_ZW" is associated with the currency "USD"; however unlike "en_US" getting a symbol of "$", the "sn_ZW" locale will return a symbol of "US $". We can't guarantee order either of the list in Locale.getAvailableLocales(); so we could get some really bizarre results. – C Nick Nov 30 '17 at 16:50
  • @CNick **sn_ZW** does not seem to be listed in Java [supported locales](http://www.oracle.com/technetwork/java/javase/documentation/java9locales-3559485.html). I wonder where do you have it from? – MaxZoom Dec 06 '17 at 16:10
  • @MaxZoom Ah yeah I was experimenting on Android with this solution. Looks like Android adds more locales to the supported Java 9 ones: https://stackoverflow.com/a/30028371/88270. This scenario might happen in another country than Zimbabwe that is supported in Java9, but I don't have an example off the top of my head where this could happen. – C Nick Apr 24 '18 at 20:27