PHP check if variable is a whole number

后端 未结 20 1166
感动是毒
感动是毒 2020-12-29 20:05

I have this PHP code:

$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;

What i want to know is, how to check whether $

20条回答
  •  天命终不由人
    2020-12-29 20:38

    What seems a simple approach would be to use modulus (%) to determine if a value is whole or not.

    x = y % 1  
    

    if y is anything other then a whole number the result is not a zero (0). A test then would be:

    if (y % 1 == 0) { 
       // this is a whole number  
    } else { 
       // this is not a whole number 
    }
    
    var isWhole = (y % 1 == 0? true: false);  // to get a boolean return. 
    

    Granted this will view a negative number as a whole number, then then just wrap ABS() around y to always test on the positive.

提交回复
热议问题