因为有些浮点数不能完全精确的表示出来。
System.out.println(3 * 0.1);
System.out.println(4 * 0.1);
System.out.println("==================");
System.out.println(1 * 0.1 == 0.1);
System.out.println(3 * 0.1 == 0.3);
System.out.println(4 * 0.1 == 0.4);
System.out.println(5 * 0.1 == 0.5);
System.out.println(9 * 0.1 == 0.9);
System.out.println(13 * 0.1 == 1.3);
System.out.println("==================");
System.out.println(3 * 0.1 / 3);
System.out.println(0.3*1);
结果:
0.30000000000000004
0.4
==================
true
false
true
true
true
true
==================
0.10000000000000002
0.3