Java- %.2f 格式说明符抛出 MissingFormatArgumentException



我正在尝试在 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)

我做错了什么?

System.out.print

参数不是("...%.2f" + x),而是("...%.2f", x)

试试这个:

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

相关内容

  • 没有找到相关文章

最新更新