PHP检查是否是整数

标签 php isinteger

我有以下计算:

$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/

相关文章:

PHP is_int 未按预期执行

PHP adodb 从一个表转储到另一个表

php - 无法确定字符串当前是否为整数

php - 使用foreach循环上传多张图片并插入数据库

php - PHP 网站中的 Facebook feed、twitter twits 和链接连接

javascript - 创建的 Number.isInteger(x) 不能在 IE 中工作

javascript - 检查字符串是否为整数

php - 当 php session 超时时执行一些事件

php - 从 MySQL 的 View 中删除记录