Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to get the symbols of the currencies based on their Locale. But instead of returning a symbol, it is returning the code. I have a snippet:

import java.util.Currency;
import java.util.Locale;

public class CurrencyFormat
{
  public void displayCurrencySymbols() 
  {
   Currency currency = Currency.getInstance(Locale.US); 
   System.out.println("United States: " + currency.getSymbol());
  } 
  public static void main(String[] args)
  {
    new CurrencyFormat().displayCurrencySymbols();
  }
}

For Locale.US it is giving symbol $ but If I replace

Currency currency = Currency.getInstance(Locale.US); 

with

Currency currency = Currency.getInstance(Locale.GERMANY); 

Then instead of symbol it is giving the country code. Why is this and how we can get the symbols?

EDIT : After looking some answer I would like to clear that setting some specific default local is not a solution as I need all the avalaible sign displayed at once.

e.g.

 Locale.setDefault(Locale.UK); 

will give me the euro sign but for doller it will give the code instead of doller sign($).

See Question&Answers more detail:os

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

1 Answer

hi please try the following code

import java.text.NumberFormat;
import java.util.Comparator;
import java.util.Currency;
import java.util.Locale;
import java.util.SortedMap;
import java.util.TreeMap;

public class CurrencyExample
{
    public static void main(String[] args) 
    {
         Utils.getCurrencySymbol( Currency.getInstance(Locale.US).getCurrencyCode());
         Utils.getCurrencySymbol(Currency.getInstance(Locale.JAPAN).getCurrencyCode());
         Utils.getCurrencySymbol(Currency.getInstance(Locale.UK).getCurrencyCode());
         Utils.getCurrencySymbol("INR");
    }
}

class Utils{
      public static SortedMap<Currency, Locale> currencyLocaleMap;
      static {
          currencyLocaleMap = new TreeMap<Currency, Locale>(new Comparator<Currency>() {
            public int compare(Currency c1, Currency c2){
                return c1.getCurrencyCode().compareTo(c2.getCurrencyCode());
            }
        });
        for (Locale locale : Locale.getAvailableLocales()) {
             try {
                 Currency currency = Currency.getInstance(locale);
             currencyLocaleMap.put(currency, locale);
             }catch (Exception e){
         }
        }
    }

    public static String getCurrencySymbol(String currencyCode) {
        Currency currency = Currency.getInstance(currencyCode);
        System.out.println( currencyCode+ ":-" + currency.getSymbol(currencyLocaleMap.get(currency)));
        return currency.getSymbol(currencyLocaleMap.get(currency));
    }
}

The output of above program is like that:

USD:-$
JPY:-¥
GBP:-£
INR:-Rs.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...