我在这里有一个非常短的代码片段:
public class FormatFloat {
public static void main(String[] args) {
float x = 12.345f;
System.out.printf("%18.7f%s", x, "\n");
System.out.printf("%18.8f%s", x, "\n");
System.out.printf("%18.10f%s", x, "\n");
System.out.printf("%18.15f%s", x, "\n");
}
}
我本以为输出是
12.3450000
12.34500000
12.3450000000
12.345000000000000
但我越来越
12.3450003
12.34500027
12.3450002670
12.345000267028809
谁能告诉我背后的原因?