在Java中以外部语言环境格式化货币 [英] Formatting Currencies in Foreign Locales in Java

查看:155
本文介绍了在Java中以外部语言环境格式化货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尽力找到一种方法,使用Java在各种不属于该货币的区域设置格式化外币。我发现java.util.Currency,它可以表示适用于各种语言环境的符号。也就是说,对于美元,它在美国给我提供了$,在其他国家给我提供了美元或美元。另外,我找到了java.text.NumberFormat,它将为特定的区域设置货币格式。我的问题 - util.Currency将提供适当的符号和代码,以货币的非默认区域设置,但不会以任何特定于区域的方式格式化货币。 NumberFormat假定使用语言环境传递它的数字是该语言环境的货币,而不是外币。例如,如果我使用getCurrencyInstance(Locale.GERMANY),然后格式化为(1000),则假定我格式化为1000欧元。实际上,我可能需要用美元,日元或任何其他货币的正确的德国本地化表示(正确的小数和千位分隔符,是否在符号之前或之后放置符号)。目前为止我所能得到的最好的结果是使用NumberFormat格式化一个数字,然后搜索非数字字符的输出,并将其替换为从util.Currency派生的符号。但是,这是非常脆弱的,可能不足以达到我的目的。想法?任何帮助深表感谢。 试试 / api / java / text / NumberFormat.html#setCurrency%28java.util.Currency%29rel =noreferrer> setCurrency

损坏

  java.text.NumberFormat format = java.text .NumberFormat.getCurrencyInstance(java.util.Locale.GERMANY); 
System.out.println(format.format(23));

输出:23,00€<

固定

  java.util.Currency usd = java.util.Currency.getInstance(USD ); 
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.GERMANY);
format.setCurrency(usd);
System.out.println(format.format(23));

输出:23,00 USD


I'm doing my best to find a way to format foreign currencies across various locales which are not default for that currency, using Java. I've found java.util.Currency, which can represent the proper symbol to use for various locales. That is, for USD, it provides me the symbol $ in the US, and US$ or USD in other nations. Also, I've found java.text.NumberFormat, which will format a currency for a specific locale. My problem - util.Currency will provide proper symbols and codes for representing currencies in their non-default locales, but will not format currency in any locale-specific way. NumberFormat assumes that the number I pass it, with a locale, is the currency of that locale, not a foreign currency.

For example, if I use getCurrencyInstance(Locale.GERMANY) and then format (1000) it assumes I am formatting 1000 euro. In reality, I may need the correct German-localized representation (correct decimal and thousands separator, whether to put the symbol before or after the amount) for USD, or Yen, or any other currency. The best I've been able to derive so far is to format a number using NumberFormat, then search the output for non-digit characters and replace them with symbols derived from util.Currency. However, this is very brittle, and probably not reliable enough for my purposes. Ideas? Any help is much appreciated.

解决方案

Try using setCurrency on the instance returned by getCurrencyInstance(Locale.GERMANY)

Broken:

java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.GERMANY);
System.out.println(format.format(23));

Output: 23,00 €

Fixed:

java.util.Currency usd = java.util.Currency.getInstance("USD");
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.GERMANY);
format.setCurrency(usd);
System.out.println(format.format(23));

Output: 23,00 USD

这篇关于在Java中以外部语言环境格式化货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆