简体   繁体   中英

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?

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 static Map 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());

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

NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
    String cur = currencyFormatter.format(yourValue);

This Formats your int/double to a currency based on the Device Language

    String currency = cur.replaceAll("[0-9.,]","");

And this is how to get just the currency Symbol, by replacing all numbers, dots and commas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM