Java 货币代码到货币符号映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3888991/
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
Currency code to currency symbol mapping
提问by artjomka
Good day, in database there is table with houses for sale records. For each house record there is currency code (in ISO 4217 format) field. Is it possibly to somehow get currency symbol from that code so I could use it on presentation side ?
美好的一天,在数据库中有出售房屋记录的表格。每个房屋记录都有货币代码(ISO 4217 格式)字段。是否可能以某种方式从该代码中获取货币符号,以便我可以在演示方面使用它?
Thank you.
谢谢你。
P.S. Was trying to resolve that problem setting Currency object (created by Currency.getInstance(currencyCode)) into DecimalNumberFormat setCurrency method and then format value I needed to display, but formatted value still without currency symbol.
PS 试图解决这个问题,将 Currency 对象(由 Currency.getInstance(currencyCode) 创建)设置为 DecimalNumberFormat setCurrency 方法,然后格式化我需要显示的值,但格式化的值仍然没有货币符号。
采纳答案by Nivas
You can use the Currency
object's getSymbolmethod.
您可以使用Currency
对象的getSymbol方法。
What symbol is used depends on the Locale which is used See thisand this.
Update, Jan 2016:The links are now dead. But they were specific to Java 1.4/5 so not really relevant anymore. More details on currency formatting can be found in https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html. The links can be found on the WayBackEngine though.
2016 年 1 月更新:链接现已失效。但它们特定于 Java 1.4/5,因此不再真正相关。有关货币格式的更多详细信息,请参见https://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html。这些链接可以在 WayBackEngine 上找到。
回答by Manjula
You can use Currency class and DecimalFormat class for achieve your requirement. In following example, # represents number and ¤ represents currency symbol, you can find relevant format parameters in java API doc for DecimalFormatclass.
您可以使用 Currency 类和 DecimalFormat 类来实现您的要求。在下面的例子中,#代表数字,¤代表货币符号,你可以在Java API文档中找到DecimalFormat类的相关格式参数。
Currency currency = Currency.getInstance("USD");
DecimalFormat decimalFormat = new DecimalFormat("#¤");
decimalFormat.setCurrency(currency);
System.out.println(decimalFormat.format(234));
回答by Kevin D
@artjomka
@artjomka
I was able to reproduce your problem by setting my default locale to Latvia
通过将默认语言环境设置为拉脱维亚,我能够重现您的问题
Locale.setDefault(new Locale("lv","LV"));
Currency c = Currency.getInstance("EUR");
System.out.println(c.getSymbol());
This gave me the output of "EUR".
这给了我“EUR”的输出。
However, by leaving setting my locale to Uk (already my default) I get the symbol for the Euro().
但是,通过将我的语言环境设置为 Uk(已经是我的默认设置),我得到了 Euro() 的符号。
Locale.setDefault(Locale.UK);
Currency c = Currency.getInstance("EUR");
System.out.println(c.getSymbol());
回答by Delfino
You should call Currency.getSymbol(Locale)
rather than Currency.getSymbol()
(without a locale object). Setting the default locale gives you the behavior that you want.
您应该调用Currency.getSymbol(Locale)
而不是Currency.getSymbol()
(没有语言环境对象)。设置默认语言环境可为您提供所需的行为。
回答by Delfino
Never blindly append currency symbols (that assumes that append is the locale-appropriate position of the currency symbol). I18n-unsafe practice. Same with the "#¤" string (for locales with prefix currency symbol, this is incorrect)
永远不要盲目地附加货币符号(假设 append 是货币符号的区域设置适当的位置)。I18n-不安全的做法。与“#¤”字符串相同(对于带有前缀货币符号的语言环境,这是不正确的)
回答by brabenetz
There is a code-Snippet in https://stackoverflow.com/a/15343675/702345
https://stackoverflow.com/a/15343675/702345 中有一个代码片段
But be careful:
不过要小心:
- One Currency can have multiple symbols. E.g.: USD has also the Symbol "US$" for Local "es_US" (at least on Java8, Win7 on my VM)
- Difference Currencies can have the same symbols, e.g.: USD, AUD
- 一种货币可以有多个符号。例如:USD 也有本地“es_US”的符号“US$”(至少在 Java8 上,我的 VM 上的 Win7)
- 差异货币可以有相同的符号,例如:美元、澳元