格式化负货币USD以在JOptionPane对话框中显示减号[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

如何让NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();以负号打印负美元货币?

我正在使用JOptionPane对话框来显示股票交易的货币输出。

NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
JOptionPane.showMessageDialog(null, "Total profit made: " 
+ defaultFormat.format(totalProfit_RH));
java currency number-formatting joptionpane currency-formatting
1个回答
0
投票

如果你想要一些与默认格式不同的东西,你就必须自己制作,但不要担心,这并不难。

DecimalFormat myFormat = new DecimalFormat("$#,##0.00;$-#,##0.00");

这将创建您自己的格式,您可以随意编辑它。如果您确实想要更改它,请谷歌“掩盖格式化java”并制作自己的面具。

总而言之,您的代码应如下所示

DecimalFormat myFormat = new DecimalFormat("$#,##0.00;$-#,##0.00");
JOptionPane.showMessageDialog(null, "Total profit made: " + myFormat.format(totalProfit_RH));
© www.soinside.com 2019 - 2024. All rights reserved.