Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locale Currency Symbol

I having some problems getting the default currency symbol of the system. I am getting the currency symbol this way:

Currency currency = Currency.getInstance(Locale.getDefault());
Log.v("TAG",currency.getSymbol());

When the system language is in English (United States) the right symbol shows up ($). But when i choose the language Portuguese (Portugal) it returns this symbol ¤.

What can be causing this?

like image 372
Filipe Batista Avatar asked Oct 02 '12 16:10

Filipe Batista


People also ask

How do I get the currency symbol from locale?

The getSymbol() method is used to get the symbol of invoking currency for the default locale.

What is locale currency?

In economics, a local currency is a currency that can be spent in a particular geographical locality at participating organisations. A regional currency is a form of local currency encompassing a larger geographical area, while a community currency might be local or be used for exchange within an online community.

How do you write currency symbols?

For US dollars, the symbol '$' is sufficient abbreviation, unless there is a mixture of dollar currencies in the text. For other dollar currencies, '$' should be prefixed with the country abbreviation. For all other currencies, write the figure first followed by the currency name, for example, '100 million yuan'.


2 Answers

This seems to be a known issue (http://code.google.com/p/android/issues/detail?id=38622. I came to a possible solution this way:

Since the problem is in the Symbol and not the Currency code, i solved this problem creating a staticMap where the key is the CurrencyCode and the value is the Symbol.

public static final Map<String, String> MYCURRENCIES = new HashMap<String, String>(){
        {
            put("EUR","€");
            put("USD","$");
            (..)
        }
};

In order to get all (or almost) the currencies codes available in the locales information you can do something like this:

for (Locale ll: Locale.getAvailableLocales()){
    try {
       Currency a = Currency.getInstance(ll);
       Log.v("MyCurrency",a.getCurrencyCode()+"#"+a.getSymbol());
    }catch (Exception e){
       // when the locale is not supported
  }
}

After you created you Map with the CurrencyCode and Symbol you just have to something like this:

Currency currency = Currency.getInstance(Locale.getDefault());
String curSymbol = MYCURRENCIES.get(currency.getCurrencyCode());
like image 124
Filipe Batista Avatar answered Oct 07 '22 20:10

Filipe Batista


Some thoughts;

  1. Could it be that you're getting the right symbol for (Euro) but your font/log doesn't have it and it only looks like that symbol?

  2. Locale is a pretty frisky class, it's constructors have no error checking. You can easily see locales that aren't supported or don't really exist in the standards (such as "de_US" for "German as spoken in the US").

  3. Note that locale data is not necessarily available for any of the locales pre-defined as constants in this class except for en_US, which is the only locale Java guarantees is always available, and it differs in different Android releases, and also can be limited by the operator or custom builds. pt_PT was added in 2.3.

  4. Regarding the option you presented, if you check out Unicode's standards as they have been implemented in API8 and up, Portugal exists only as a territory (and not the combination).

  5. It may be better if you therefore create a partial locale instance from the country code alone, and then get the currency symbol for Portugal for instance. You will not be able to comply with the predefined statics of Locale's class as it pretty narrowly supports different locales (It's a short list), but it should work as this locale has the data you are looking for in the system.

  6. I would also try and see if currency.toString() returns the EUR (the ISO 4217 code of the currency, usually a three letter acronym).

like image 39
Ben Max Rubinstein Avatar answered Oct 07 '22 20:10

Ben Max Rubinstein