【问题标题】:Format negative currency of USD to display a minus sign in a JOptionPane dialog box [duplicate]格式化美元的负货币以在 JOptionPane 对话框中显示减号 [重复]
【发布时间】:2018-02-28 18:37:34
【问题描述】:

如何让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】:

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

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

    这将创建您自己的格式,您可以随意编辑它。如果您确实想更改它,请在 Google 上搜索“掩码格式化 java”并制作您自己的掩码。

    总而言之,你的代码应该是这样的

    DecimalFormat myFormat = new DecimalFormat("$#,##0.00;$-#,##0.00");
    JOptionPane.showMessageDialog(null, "Total profit made: " + myFormat.format(totalProfit_RH));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多