3*0.1 == 0.3 将会返回什么?true 还是 false?

false,因为有些浮点数不能完全精确的表示出来
例如
public class floatceshi {
    public static void main(String[] args) {
        System.out.println(3 * 0.1);
        System.out.println(4 * 0.1);
        System.out.println(3 * 0.1 == 0.3);
        System.out.println(13 * 0.1 == 1.3);
        System.out.println(9 * 0.1 == 0.9);
        System.out.println(3 * 0.1 / 3);
    }
}

 

结果:

0.30000000000000004
0.4
false
true
true
0.10000000000000002

 

 

知乎上的一个解释

 

posted @ 2019-06-11 12:02  那些年的代码  阅读(1184)  评论(0编辑  收藏  举报