简体   繁体   English

PHP检查是否为整数

[英]PHP check if is integer

I have the following calculation: 我有以下计算:

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

Now I want to check if my variable $calc is integer (has decimals) or not. 现在我想检查我的变量$calc是否为整数(有小数)。

I tried doing if(is_int()) {} but that doesn't work because $calc = (float)123 . 我尝试了if(is_int()) {}但是这不起作用,因为$calc = (float)123

Also tried this- 也试过这个 -

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

but that also doesn't work because it returns in every case 'is float' . 但这也行不通,因为它在每种情况下都返回'is float' In the case above that should'n be true because 123 is the same as 123 after rounding. 在上面的情况下,这应该是真的,因为在舍入后123与123相同。

Try- 尝试-

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

Demo 演示

As CodeBird pointed out in a comment to the question, floating points can exhibit unexpected behaviour due to precision "errors". 正如CodeBird在对该问题的评论中指出的那样,由于精度“错误”,浮点可能会出现意外行为。

eg 例如

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

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

prints on my machine (win8, x64 but 32bit build of php) 在我的机器上打印(win8,x64但是32位构建的php)

0.9 0.9 no

took a while to find a (hopefully correct) example that is a) relevant to this question and b) obvious (I think x / y * y is obvious enough). 花了一段时间才找到一个(希望是正确的)例子,这个例子是a)与这个问题相关,b)显而易见(我认为x / y * y很明显)。

again this was tested on a 32bit build on a 64bit windows 8 再次,这是在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);

and the output is 而输出是

x=5
bool(false)

Depending on what you're trying to achieve it might be necessary to check if the value is within a certain range of an integer (or it might be just a marginalia on the other side of the spectrum ;-) ), eg 根据您要实现的目标,可能需要检查该值是否在整数的某个范围内(或者它可能只是频谱另一侧的边际;-)),例如

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

and you might also take a look at some arbitrary precision library, eg the bcmath extension where you can set "the scale of precision". 您还可以查看一些任意精度库,例如bcmath扩展 ,您可以在其中设置“精度等级”。

round() will return a float. round()将返回一个浮点数。 This is because you can set the number of decimals. 这是因为您可以设置小数位数。

You could use a regex: 你可以使用正则表达式:

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

PHP will convert $calc automatically into a string when passing it to preg_match() . 当将$calc传递给preg_match()时,PHP会自动将$calc转换为字符串。

You can do it using ((int) $var == $var) 你可以用((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;

Basically you check if the int value of $var equal to $var 基本上你检查$var的int值是否等于$var

You can use number_format() to convert number into correct format and then work like this 您可以使用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 演示

Ok I guess I'am pretty late to the party but this is a alternative using fmod() which is a modulo operation. 好吧,我想我已经很晚了,但这是一个使用fmod()的替代方案,这是一个模运算。 I simply store the fraction after the calculation of 2 variables and check if they are > 0 which would imply it is a float. 我只是在计算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 Eval在这里

Edit: Reminder Fmod will use a division to compare numbers the whole documentation can be found here 编辑:提醒Fmod将使用一个部门来比较数字, 这里可以找到整个文档

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

Try this: 试试这个:

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

you may use the is_int() function at the place of round() function. 你可以在is_int()函数的地方使用is_int() round()函数。

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

I think it would help you 我认为这会对你有所帮助

A more unorthodox way of checking if a float is also an integer: 检查浮点数是否也是整数的更为非正统的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM