语言环境货币符号

时间:2023-04-19
本文介绍了语言环境货币符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在获取系统的默认货币符号时遇到了一些问题.我以这种方式获取货币符号:

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

当系统语言为英语(美国)时,右侧的符号会显示($).但是当我选择语言 Portuguese (Portugal) 它返回这个符号 ¤.

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

这是什么原因造成的?

推荐答案

这似乎是一个已知问题(http://code.google.com/p/android/issues/detail?id=38622.我通过这种方式得出了一个可能的解决方案:

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:

由于问题出在符号而不是货币代码中,我解决了这个问题,创建了一个静态Map,其中键是 CurrencyCode,值是 符号.

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
  }
}

使用 CurrencyCodeSymbol 创建地图后,您只需要这样:

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

这篇关于语言环境货币符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:javax.el.PropertyNotWritableException:/index.xhtml @29,118 va 下一篇:如何使用 DateTimeFormatter 解析非标准月份名称

相关文章

最新文章