Get the currency symbol based on country

↘锁芯ラ 提交于 2019-12-01 07:09:11

问题


I have a TextView which display currencies. By default my textview's text is: $0.00 How can I make it so the $ changes based on user 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();

Which shows $ 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 shows AU$ instead of $

How can I set the currency symbol without all the extra stuff?


回答1:


If what you want is to add that format to a number you have you could do

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

for default or

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

for specified




回答2:


You could use a regular expression to remove all word characters.

    String symbol = currency.getSymbol().replaceAll("\\w", "");

However this may not be ideal if any of the monetary symbols you are dealing with use letters.



来源:https://stackoverflow.com/questions/18260767/get-the-currency-symbol-based-on-country

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!