【问题标题】:Java- %.2f format specifier throwing MissingFormatArgumentExceptionJava- %.2f 格式说明符抛出 MissingFormatArgumentException
【发布时间】:2015-04-19 21:37:04
【问题描述】:

我正在尝试在 Java 程序中使用格式说明符来仅显示我正在打印的 double 变量的前 2 位小数。

现在,我打印double 的语句是System.out.format("Total = $%.2f" + value);(值是有问题的双精度)。

当我运行我的程序时,它会毫无问题地打印“Total = $”,然后立即给出MissingFormatArgumentException

控制台中的完整错误日志:

Total = $Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%.3f'
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at Account.getBalance(Account.java:21)
    at testAccount.main(testAccount.java:4)

我做错了什么?

【问题讨论】:

  • 你能提供你的代码吗?
  • @Filburt:我回滚了你的编辑,因为根本没有任何改进......它只会让情况变得更糟。
  • @Tom Andy 祈祷以何种方式将错误消息格式化为块引用而不是代码格式化会使情况变得更糟?
  • @Filburt 在我看来,块引用应该只用于引用文本,而不是源代码。问题是,没有格式和缩进。在这种情况下(your edit),堆栈跟踪“更难”阅读。

标签: java format-specifiers


【解决方案1】:

System.out.print的参数不是("...%.2f" + x),而是("...%.2f", x)

试试这个:

System.out.format("Total = $%.2f", value);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2015-01-16
    • 2016-02-13
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多