gpt4 book ai didi

PHP检查是否是整数

转载 作者:可可西里 更新时间:2023-11-01 13:36:59 25 4
gpt4 key购买 nike

我有以下计算:

$this->count = float(44.28)
$multiple = float(0.36)
$calc = $this->count / $multiple;
$calc = 44.28 / 0.36 = 123

现在我想检查我的变量 $calc 是否为整数(有小数)。

我尝试执行 if(is_int()) {} 但它不起作用,因为 $calc = (float)123

也试过这个-

if($calc == round($calc)) 
{
die('is integer');
}
else
{
die('is float);
}

但这也不起作用,因为它在每种情况下都返回 'is float'。在上面的例子中,这不应该是真的,因为 123 与四舍五入后的 123 相同。

最佳答案

尝试-

if ((string)(int) $calc === (string)$calc) {
//it is an integer
}else{
//it is a float
}

Demo

关于PHP检查是否是整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22607441/

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