gpt4 book ai didi

java - 如何解决 IllegalFormatConversionException

转载 作者:行者123 更新时间:2023-12-01 07:55:11 26 4
gpt4 key购买 nike

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.3Exception in thread "main" java.util.IllegalFormatConversionException: o != java.lang.Double at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source) at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source) at java.util.Formatter$FormatSpecifier.print(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.io.PrintStream.format(Unknown Source) at java.io.PrintStream.printf(Unknown Source) at Experiments.main(Experiments.java:6)

最佳答案

如果您想在格式字符串中使用百分号,则需要对它们进行转义:

System.out.printf("1.3%% of 950 is %1.2f", x);

现在,Java 尝试使用 %o (八进制表示法)格式化 double 值,但这仅对整数有效。

关于java - 如何解决 IllegalFormatConversionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31312903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com