5

我有以下计算:

$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 相同。

4

10 回答 10

9

尝试-

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

演示

于 2014-03-24T11:02:12.277 回答
3

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

例如

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

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

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

0.9 0.9 no

花了一段时间才找到一个(希望是正确的)示例,该示例a)与该问题相关并且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 扩展,您可以在其中设置“精度范围”。

于 2014-03-24T11:52:29.107 回答
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();

在这里评估

编辑:提醒 Fmod 将使用除法来比较数字整个文档可以在这里找到

于 2014-03-24T12:05:25.943 回答
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");

演示

于 2014-03-24T11:42:35.190 回答
1

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

您可以使用正则表达式:

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

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

于 2014-03-24T11:00:47.493 回答
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;

基本上你检查 int 值是否$var等于$var

于 2014-03-24T11:03:23.840 回答
0

尝试这个:

//$calc = 123;
$calc = 123.110;
if(ceil($calc) == $calc)
{
    die("is integer");
}
else
{
    die("is float");
}
于 2014-03-24T11:15:23.187 回答
0

你可以在is_int()函数的地方使用round()函数。

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

我想它会帮助你

于 2014-03-24T11:20:59.217 回答
0
if (empty($calc - (int)$calc))
{
    return true; // is int
}else{
    return false; // is no int
}
于 2014-03-24T11:05:35.657 回答
0

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

// ctype returns bool from a string and that is why use strval
$result = ctype_digit(strval($float));
于 2015-10-31T18:38:37.903 回答