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