java 地区货币符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12694192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:51:14  来源:igfitidea点击:

Locale Currency Symbol

javaandroidlocalizationinternationalizationcurrency

提问by Filipe Batista

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

当系统语言在English (United States)正确的符号出现时($)。但是当我选择语言时,Portuguese (Portugal)它会返回这个符号¤

What can be causing this?

什么可能导致这种情况?

回答by Filipe Batista

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:

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

Since the problem is in the Symbol and not the Currency code, i solved this problem creating a staticMapwhere the key is the CurrencyCodeand the value is the Symbol.

由于问题出在 Symbol 而不是 Currency 代码中,我解决了这个问题,创建了一个静态Map,其中键是CurrencyCode,值是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 CurrencyCodeand Symbolyou just have to something like this:

使用CurrencyCodeSymbol创建 Map 后,您只需执行以下操作:

Currency currency = Currency.getInstance(Locale.getDefault());
String curSymbol = MYCURRENCIES.get(currency.getCurrencyCode());

回答by Ben Max Rubinstein

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

  1. 可能是您获得了正确的(欧元)符号,但您的字体/日志没有它,它看起来只是那个符号?

  2. Locale 是一个非常活泼的类,它的构造函数没有错误检查。您可以很容易地看到标准中不支持或不存在的语言环境(例如“de_US”表示“在美国使用的德语”)。

  3. 请注意,除了 en_US 外,区域设置数据不一定可用于此类中的任何预定义为常量的区域设置,这是 Java 保证始终可用的唯一区域设置,并且在不同的 Android 版本中有所不同,并且还可以通过以下方式进行限制操作员或自定义构建。pt_PT 是在 2.3 中添加的

  4. 关于您提供的选项,如果您查看Unicode 标准,因为它们已在 API8 及更高版本中实现,葡萄牙仅作为一个领土存在(而不是组合)。

  5. 因此,如果您仅从国家/地区代码创建部分区域设置实例,然后获取葡萄牙的货币符号,则可能会更好。您将无法遵守Locale 类的预定义静态,因为它非常狭隘地支持不同的语言环境(这是一个简短的列表),但它应该可以工作,因为该语言环境具有您在系统中查找的数据。

  6. 我还会尝试查看currency.toString() 是否返回欧元(货币的ISO 4217 代码,通常是三个字母的首字母缩写词)。

回答by ORY

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

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

这将您的 int/double 格式化为基于设备语言的货币

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

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

这就是如何通过替换所有数字、点和逗号来获得货币符号