The right way to compare a System.Double to '0' (a number, int?)

前端 未结 6 2044
遥遥无期
遥遥无期 2020-12-08 03:44

Sorry, this might be a easy stupid question, but I need to know to be sure.

I have this if expression,

void Foo()
{
    System.Double so         


        
6条回答
  •  抹茶落季
    2020-12-08 04:23

    I dont' think it's equal, honestly. Consider yuor own example: something = 0.9, or 0.0004. In first case it will be FALSE, in second case it will be TRUE. Dealing with this types I usually define for me precision percentage and compare within that precision. Depends on your needs. something like...

    if(((int)(something*100)) == 0) {
    
    
    //do something
    }
    

    Hope this helps.

提交回复
热议问题