开发者

How to determine if a digit divided by 8 is an integer?

How do I determine if a digit divided by 8 is an integer?

for example:

32 % 8 = 4, it is an integer.

25 % 8 = 3.125, it is not an integer.

I need a working code like:

if ($str % 8 == integer){
   // continue workin开发者_如何学Cg
}


the % operator, modulo, will ALWAYS return an integer - it's the remainder of the division.

If you mean you want to check if a number is evenly divisible by 8, then do

if ($str % 8 == 0) {
   ... evenly divisible by 8 ...
}


you can go with if (val % 8 == 0) or with more tricky ways, like val & 0x0FFF == 0 by using bitwise operators.

Somehow they work in the same way: first snippet checks if the remainder of the division by 8 is zero while second checks if number doesn't have any binary digit for 1, 2, or 4, which would make the number not divisible by 8.


I think this is what you need:

if(is_int($integer)) {
    // do something with integer
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜