简体   繁体   English

Java默认NumberFormat货币符号

[英]Java Default NumberFormat Currency symbol

I'm using NumberFormat's format() method to format currency in my application. 我正在使用NumberFormat的format()方法来格式化应用程序中的货币。 When obtaining the currency instance, I'm not passing any locale to the method. 获取货币实例时,我没有将任何语言环境传递给该方法。 I've deployed this code to my app cluster which contains two nodes/servers. 我已将此代码部署到包含两个节点/服务器的应用程序集群中。 Interestingly, on one of the app server, the formattedAmmt is $xxxx.xx but on the other one it is ¤xxxx.xx. 有趣的是,在其中一台应用程序服务器上,formattedAmmt是$ xxxx.xx,而在另一台上是¤xxxx.xx。 As far as I understand, this character is a universal currency symbol and JDK uses this when no particular locale is available. 据我了解,此字符是通用货币符号,当没有特定的语言环境可用时,JDK使用此字符。 Is my understanding correct? 我的理解正确吗? If yes, how come it is working on one of the app server but not on the other? 如果是,它如何在其中一台应用程序服务器上运行,而不在另一台应用程序上运行? We are not seeing any default locale or such properties in app server/JVM properties. 我们在应用服务器/ JVM属性中没有看到任何默认语言环境或此类属性。

double amount = xxxx.xx;
String formattedAmt = NumberFormat.getCurrencyInstance().format(amount);

PS: I'm deploying this app to WebSphere app server cluster which is using JDK 1.6. PS:我正在将此应用程序部署到使用JDK 1.6的WebSphere应用程序服务器集群。

Based on the java doc : https://docs.oracle.com/javase/6/docs/api/java/text/NumberFormat.html#getCurrencyInstance() 基于Java doc: https : //docs.oracle.com/javase/6/docs/api/java/text/NumberFormat.html#getCurrencyInstance()

public static final NumberFormat getCurrencyInstance() Returns a currency format for the current default locale.

It will base on the current locale of the server. 它将基于服务器的当前区域设置。 That's why you may have two differents behavior. 这就是为什么您可能会有两种不同的行为。

If you want to specify the local, you have to use the following method: 如果要指定本地,则必须使用以下方法:

public static NumberFormat getCurrencyInstance(Locale inLocale) Returns a currency format for the specified locale

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM