How to test if a double is zero?

前端 未结 5 1000
别跟我提以往
别跟我提以往 2020-12-08 18:32

I have some code like this:

class Foo {
    public double x;
}

void test() {
    Foo foo = new Foo();

    // Is this a valid way to test for zero? \'x\' ha         


        
5条回答
  •  鱼传尺愫
    2020-12-08 19:08

    In Java, 0 is the same as 0.0, and doubles default to 0 (though many advise always setting them explicitly for improved readability). I have checked and foo.x == 0 and foo.x == 0.0 are both true if foo.x is zero

提交回复
热议问题