public class Experiments {
public static void main(String[] args) {
double x;
x = (1.3/100)*950;
System.out.printf("1.3% of 950 is %1.2f", x);
}
}
运行程序说:
1.3线程“main”中的异常java.util.IllegalFormatConversionException:o!= java.lang.Double at java.util.Formatter $ FormatSpecifier.failConversion(未知来源)at java.util.Formatter $ FormatSpecifier.printInteger(未知来源)at java.util.Formatter $ FormatSpecifier.print(未知来源)at java.util.Formatter.format(未知来源)at java.io.PrintStream.format(未知来源)at java.io.PrintStream.printf(未知来源)at Experiments.main(Experiments.java:6)
答案 0 :(得分:6)
如果您想在格式字符串中使用百分号,则需要将其转义:
System.out.printf("1.3%% of 950 is %1.2f", x);
现在,Java尝试使用%o
(八进制表示法)格式化double,但这仅对整数有效。