检查一个数字是否可以被 6 PHP 整除 [英] checking if a number is divisible by 6 PHP

查看:58
本文介绍了检查一个数字是否可以被 6 PHP 整除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个数字是否能被 6 整除,如果不能,我需要增加它直到它被整除.

I want to check if a number is divisible by 6 and if not I need to increase it until it becomes divisible.

我该怎么做?

推荐答案

if ($number % 6 != 0) {
  $number += 6 - ($number % 6);
}

模数 运算符给出除法,所以 $number % 6 是除以 6 时剩下的数量.这比循环并不断重新检查要快.

The modulus operator 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.

如果减少是可以接受的,那么这会更快:

If decreasing is acceptable then this is even faster:

$number -= $number % 6;

这篇关于检查一个数字是否可以被 6 PHP 整除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆