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());
当系统语言为英语(美国)时,右侧的符号会显示($).但是当我选择语言 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
}
}
使用 CurrencyCode 和 Symbol 创建地图后,您只需要这样:
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());
这篇关于语言环境货币符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:语言环境货币符号
基础教程推荐
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
