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($).
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()
:By changing your default locale:
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.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:
hi please try the following code
The output of above program is like that: