检查一个数字是否可以被 6 PHP 整除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2090475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
checking if a number is divisible by 6 PHP
提问by Utku Dalmaz
I want to check if a number is divisible by 6 and if not I need to increase it until it becomes divisible.
我想检查一个数字是否可以被 6 整除,如果不是,我需要增加它直到它被整除。
how can I do that ?
我怎样才能做到这一点 ?
回答by ZoFreX
if ($number % 6 != 0) {
$number += 6 - ($number % 6);
}
The modulusoperator gives the remainder of the division, so $number % 6 is the amount left over when dividing by 6. This will be faster than doing a loop and continually rechecking.
的模数运算符给出了除法的余数,所以数目$%6是由6。该分割时会比做一个循环,并连续地重新检查更快遗留量。
If decreasing is acceptable then this is even faster:
如果减少是可以接受的,那么这会更快:
$number -= $number % 6;
回答by Bandi-T
if ($variable % 6 == 0) {
echo 'This number is divisible by 6.';
}:
Make divisible by 6:
被 6 整除:
$variable += (6 - ($variable % 6)) % 6; // faster than while for large divisors
回答by Ponkadoodle
$num += (6-$num%6)%6;
no need for a while loop! Modulo (%) returns the remainder of a division. IE 20%6 = 2. 6-2 = 4. 20+4 = 24. 24 is divisible by 6.
不需要while循环!模数 (%) 返回除法的余数。即 20%6 = 2. 6-2 = 4. 20+4 = 24. 24 可被 6 整除。
回答by zneak
回答by mickmackusa
I see some of the other answers calling the modulo twice.
我看到其他一些答案两次调用模数。
My preference is not to ask php to do the same thing more than once. For this reason, I cache the remainder.
我的偏好是不要让 php 多次做同样的事情。出于这个原因,我缓存了剩余部分。
Other devs may prefer to not generate the extra global variable or have other justifications for using modulo operator twice.
其他开发人员可能更喜欢不生成额外的全局变量,或者有其他理由来两次使用模运算符。
Code: (Demo)
代码:(演示)
$factor = 6;
for($x = 0; $x < 10; ++$x){ // battery of 10 tests
$number = rand( 0 , 100 );
echo "Number: $number Becomes: ";
if( $remainder = $number % $factor ) { // if not zero
$number += $factor - $remainder; // use cached $remainder instead of calculating again
}
echo "$number\n";
}
Possible Output:
可能的输出:
Number: 80 Becomes: 84
Number: 57 Becomes: 60
Number: 94 Becomes: 96
Number: 48 Becomes: 48
Number: 80 Becomes: 84
Number: 36 Becomes: 36
Number: 17 Becomes: 18
Number: 41 Becomes: 42
Number: 3 Becomes: 6
Number: 64 Becomes: 66
回答by Mitch Wheat
回答by James Simpson
Simply run a while loop that will continue to loop (and increase the number) until the number is divisible by 6.
只需运行一个 while 循环,该循环将继续循环(并增加数字),直到数字可被 6 整除。
while ($number % 6 != 0) {
$number++;
}
回答by martinr
Assuming $foois an integer:
假设$foo是一个整数:
$answer = (int) (floor(($foo + 5) / 6) * 6)
回答by Chris Jester-Young
For micro-optimisation freaks:
对于微优化怪胎:
if ($num % 6 != 0)
$num += 6 - $num % 6;
More evaluations of %, but less branching/looping. :-P
更多的 评估%,但更少的分支/循环。:-P
回答by Prasoon Saurav
Why don't you use the Modulus Operator?
为什么不使用模数运算符?
Try this:
尝试这个:
while ($s % 6 != 0) $s++;
Or is this what you meant?
或者这就是你的意思?
<?
$s= <some_number>;
$k= $s % 6;
if($k !=0) $s=$s+6-$k;
?>

