Java – Gets the country-based currency symbol

Gets the country-based currency symbol… here is a solution to the problem.

Gets the country-based currency symbol

I have a TextView that displays currency. By default, the text for my TextView is: $0.00
How can I make the $ change based on the user’s selection.

I have the following code:

Locale locale=new Locale("en", "US");
Currency currency=Currency.getInstance(locale);
String symbol = currency.getSymbol();
Toast.makeText(getActivity(), symbol, Toast.LENGTH_SHORT).show();

Show $ but if I have the following:

Locale locale=new Locale("en", "AU");
Currency currency=Currency.getInstance(locale);
String symbol = currency.getSymbol();
Toast.makeText(getActivity(), symbol, Toast.LENGTH_SHORT).show();

It displays AU$ instead of $

How do I set a currency symbol without all the extras?

Solution

If what you want is to add that format to the numbers you can do

myString = NumberFormat.getCurrencyInstance().format(myNumber);

Default.
or

myString = NumberFormat.getCurrencyInstance(new Locale("en", "AU")).format(myNumber);

specified

Related Problems and Solutions