我正在尽我最大的努力寻找一种方法,使用 Java 来在各种语言环境中格式化外币,这些语言不是该货币的默认设置.我找到了 java.util.Currency,它可以代表用于各种语言环境的正确符号.也就是说,对于美元,它在美国为我提供符号 $,在其他国家提供美元或美元.此外,我还找到了 java.text.NumberFormat,它将为特定语言环境格式化货币.我的问题 - util.Currency 将提供适当的符号和代码来表示其非默认语言环境中的货币,但不会以任何特定于语言环境的方式格式化货币.NumberFormat 假定我传递给它的数字(带有语言环境)是该语言环境的货币,而不是外币.
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.
例如,如果我使用 getCurrencyInstance(Locale.GERMANY) 然后格式化 (1000) 它假定我正在格式化 1000 欧元.实际上,对于美元、日元或任何其他货币,我可能需要正确的德语本地化表示(正确的小数和千位分隔符,无论是在金额之前还是之后放置符号).到目前为止,我能够得出的最好的结果是使用 NumberFormat 格式化数字,然后在输出中搜索非数字字符并将它们替换为从 util.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.
尝试使用setCurrency 在 getCurrencyInstance(Locale.GERMANY) 返回的实例上
Try using setCurrency on the instance returned by getCurrencyInstance(Locale.GERMANY)
坏了:
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.GERMANY);
System.out.println(format.format(23));
产出:23,00 欧元
Output: 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 美元
Output: 23,00 USD
这篇关于在 Java 中的外国语言环境中格式化货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何检测 32 位 int 上的整数溢出?How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
return 语句之前的局部变量,这有关系吗?Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
如何将整数转换为整数?How to convert Integer to int?(如何将整数转换为整数?)
如何在给定范围内创建一个随机打乱数字的 intHow do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
java的行为不一致==Inconsistent behavior on java#39;s ==(java的行为不一致==)
为什么 Java 能够将 0xff000000 存储为 int?Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)