gpt4 book ai didi

php - PHP 中如何除以 float ?

转载 作者:行者123 更新时间:2023-12-02 00:49:26 24 4
gpt4 key购买 nike

我需要确定PHP中给定的数字x是否能被y整除,y 是一个 float ,具体为 0.05。

但是当我这样做的时候,

if ($number % 0.05 === 0) {
// Continue
}

我收到以下 PHP 警告:

Division by zero

我可以做什么来解决这个问题?谢谢!

最佳答案

模数的定义如下:

In computing, the modulo (sometimes called modulus) operation finds the remainder of division of one number by another.

表达a % b的另一种方式是:a - b * Floor(a/b)

因此,您可以定义自己的函数:

function mod($a,$b) {return $a-$b*floor($a/$b);}

然后简单地调用它。例如,调用 mod(0.08,0.05) 将返回 0.03

关于php - PHP 中如何除以 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15148439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com