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

Posted 醉酒的小男人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3*0.1 == 0.3 将会返回什么?true 还是 false?相关的知识,希望对你有一定的参考价值。

3*0.1 == 0.3 返回 true还是 false

返回false因为有些浮点数不能完全精确的表示出来。

例如:

 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

知乎上的一个解释

以上是关于3*0.1 == 0.3 将会返回什么?true 还是 false?的主要内容,如果未能解决你的问题,请参考以下文章