Joachim Vanthuyne
Joachim Vanthuyne

Reputation: 355

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.

I tried doing if(is_int()) {} but that doesn't work because $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'. In the case above that should'n be true because 123 is the same as 123 after rounding.

Upvotes: 5

Views: 554

Answers (10)

Elzo Valugi
Elzo Valugi

Reputation: 27856

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));

Upvotes: 0

cptnk
cptnk

Reputation: 2420

Ok I guess I'am pretty late to the party but this is a alternative using fmod() which is a modulo operation. I simply store the fraction after the calculation of 2 variables and check if they are > 0 which would imply it is a float.

<?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

Edit: Reminder Fmod will use a division to compare numbers the whole documentation can be found here

Upvotes: 1

krishna
krishna

Reputation: 4099

You can use number_format() to convert number into correct format and then work like this

$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

Upvotes: 1

VolkerK
VolkerK

Reputation: 96159

As CodeBird pointed out in a comment to the question, floating points can exhibit unexpected behaviour due to precision "errors".

e.g.

<?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)

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).

again this was tested on a 32bit build on a 64bit windows 8

<?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 ;-) ), e.g.

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, e.g. the bcmath extension where you can set "the scale of precision".

Upvotes: 3

hek2mgl
hek2mgl

Reputation: 157947

round() will return a float. 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().

Upvotes: 1

Rahul Goyal
Rahul Goyal

Reputation: 203

you may use the is_int() function at the place of round() function.

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

I think it would help you

Upvotes: 0

Sahil Mittal
Sahil Mittal

Reputation: 20753

Try-

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

Demo

Upvotes: 9

Nikunj Kabariya
Nikunj Kabariya

Reputation: 840

Try this:

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

Upvotes: 0

Dylan Westra
Dylan Westra

Reputation: 631

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

Upvotes: 0

darkheir
darkheir

Reputation: 8950

You can do it using ((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

Upvotes: 1

Related Questions