Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting currency symbols for specific Locale

Tags:

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

like image 248
Ram Dutt Shukla Avatar asked Feb 27 '13 07:02

Ram Dutt Shukla


People also ask

How do I get the currency symbol from locale?

The getSymbol() method is used to get the symbol of a given currency for the default DISPLAY 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.

How do you type different currency symbols?

123 key in the lower-left corner of the keyboard. In the second row of numbers and symbols, press and hold your finger on the dollar-sign key. Above your finger, a box will pop up showing several currency symbols; these include the peso, the euro, the cent sign, the pound sterling and the yen.

Where do currency codes go?

If the three-letter currency code (ISO 4217) is used, it ALWAYS goes in front of the value. If currency symbols are used, some countries put the symbol in front, others behind the value.


2 Answers

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. 
like image 104
Amrit Raj Sharma Avatar answered Oct 16 '22 22:10

Amrit Raj Sharma


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.

like image 21
Duncan Jones Avatar answered Oct 16 '22 21:10

Duncan Jones