Not getting currency symbols for specific Locale

2020-02-02 08:48发布

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($).

3条回答
不美不萌又怎样
2楼-- · 2020-02-02 09:08

You are seeing what Java thinks users in your default locale expect to see for a currency symbol. This is also confirmed by the Javadocs for 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.

By changing your default locale:

Locale.setDefault(Locale.UK); // e.g.

You can experiment to prove this for yourself. (Note: I'm not suggesting this is a solution, merely an opportunity for you to check what other locales see).

I would recommend you stick with whatever Java thinks is appropriate for your default locale - I'm sure it's understandable by your users.


It might seem attractive to seek the "normal" currency symbol for every currency, however take a look at the following currency list: http://www.xe.com/symbols.php.

Look how many countries recognise their currency with a $ symbol. Egypt also uses the British pound symbol (£). The whole idea behind the locale currency symbol is to give you a string that users in your locale will understand.

查看更多
Animai°情兽
3楼-- · 2020-02-02 09:09

A simpler solution, based on Amrit Raj Sharma's answer:

Just add the locale as a parameter to the getSymbol() method.

So, change your code to:

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(Locale.US));
  } 
  public static void main(String[] args)
  {
    new CurrencyFormat().displayCurrencySymbols();
  }
}
查看更多
别忘想泡老子
4楼-- · 2020-02-02 09:16

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.
查看更多
登录 后发表回答