PHP检查是否为整数

时间:2014-03-24 10:58:10

标签: 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相同。

10 个答案:

答案 0 :(得分:9)

尝试 -

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

Demo

答案 1 :(得分:3)

正如CodeBird在对该问题的评论中所指出的,由于精度和错误,浮点可能会出现意外行为。

e.g。

<?php
$x = 1.4-0.5;
$z = 0.9;

echo $x, ' ', $z, ' ', $x==$z ? 'yes':'no';

在我的机器上打印(win8,x64但是32位构建的php)

0.9 0.9 no

需要一段时间才能找到一个(希望是正确的)示例,该示例与此问题相关,并且b)明显(我认为x / y * y足够明显)。

再次,这是在64位Windows 8上的32位构建上进行测试

<?php
$y = 0.01; // some mambojambo here... 
for($i=1; $i<31; $i++) { // ... because ...
    $y += 0.01; // ... just writing ...
} // ... $y = 0.31000 didn't work

$x = 5.0 / $y;
$x *= $y;

echo 'x=', $x, "\r\n";
var_dump((int)$x==$x);

,输出

x=5
bool(false)

根据您尝试实现的目标,可能需要检查该值是否在整数的某个范围内(或者它可能只是频谱另一侧的边际值;-)) ,例如

function is_intval($x, $epsilon = 0.00001) {
    $x = abs($x - round($x));
    return $x < $epsilon;
};

您可能还会看一些任意精度库,例如: bcmath extension您可以在其中设置&#34;精度等级&#34;。

答案 2 :(得分:1)

round()将返回一个浮点数。这是因为您可以设置小数位数。

您可以使用正则表达式:

if(preg_match('~^[0-9]+$~', $calc))

$calc传递给preg_match()时,PHP会自动转换为字符串。

答案 3 :(得分:1)

您可以使用((int) $var == $var)

执行此操作
$var = 9;
echo ((int) $var == $var) ? 'true' : 'false';
//Will print true;
$var = 9.6;
echo ((int) $var == $var) ? 'true' : 'false';
//Will print false;

基本上,您检查$var的int值是否等于$var

答案 4 :(得分:1)

您可以使用number_format()将数字转换为正确的格式,然后像这样工作

$count = (float)(44.28);

$multiple = (float)(0.36);

$calc = $count / $multiple;
//$calc = 44.28 / 0.36 = 123

$calc = number_format($calc, 2, '.', '');

if(($calc) == round($calc))
die("is integer");
else
die("is not integer");

Demo

答案 5 :(得分:1)

好吧,我想我已经很晚了,但这是一个使用fmod()的替代方案,这是一个模运算。我只是在计算2个变量后存储该分数并检查它们是否> 0意味着它是一个浮动。

<?php

  class booHoo{
     public function __construct($numberUno, $numberDos) {
        $this->numberUno= $numberUno;
        $this->numberDos= $numberDos;
     }

     public function compare() {
       $fraction = fmod($this->numberUno, $this->numberDos);
       if($fraction > 0) {
         echo 'is floating point';
       } else {
         echo 'is Integer';
       }
     }
   }

$check= new booHoo(5, 0.26);
$check->compare();

Eval here

修改:提醒Fmod将使用分部来比较可以找到整个文档的数字here

答案 6 :(得分:0)

if (empty($calc - (int)$calc))
{
    return true; // is int
}else{
    return false; // is no int
}

答案 7 :(得分:0)

试试这个:

//$calc = 123;
$calc = 123.110;
if(ceil($calc) == $calc)
{
    die("is integer");
}
else
{
    die("is float");
}

答案 8 :(得分:0)

您可以在is_int()功能的位置使用round()功能。

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

我认为这会对你有所帮助

答案 9 :(得分:0)

一种更为非正统的检查浮点数是否也是整数的方法:

// ctype returns bool from a string and that is why use strval
$result = ctype_digit(strval($float));
相关问题