5

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.

14
  • 1
    try is_int() instead of your code Commented Mar 24, 2014 at 10:59
  • You could also use Modolo '%' if you are hipster enough. Commented Mar 24, 2014 at 11:00
  • Why not use is_float? Commented Mar 24, 2014 at 11:00
  • 1
    why do you need to know in the first place? what do you want todo? Commented Mar 24, 2014 at 11:30
  • 1
    ceil the value without knowing :D if it is an int it won't change anything if it is a float it will ceil... more efficient than using if's Commented Mar 24, 2014 at 11:34

10 Answers 10

9

Try-

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

Demo

Sign up to request clarification or add additional context in comments.

10 Comments

This will not work, as it would return true even for 1 == 1.00 . (Or I misunderstood the question)
@Rufinus : don't make it === because $calc is still a float/double and === also tests the type.
this won't work becuse of float calculation precision, check here: eval.in/125555
@VolkerK $foo = intval(1); echo ((int) $foo === $foo) ? 'int' : 'something else'; $bar = floatval(1.23); echo ((int) $bar === $bar) ? 'int' : 'something else'; will print "int" and "something else"
i dont see your problem echo ceil(12); echo ceil(12.0); echo ceil(12.1); output ist 12, 12, 13. (PHP 5.5.3)
|
3

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

Comments

1

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

Comments

1

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

11 Comments

is_int() or is_float() is better for this purpose
@krishna is_int() will not work as the result of round() is a float. You are wrong.
@hek2mgl if the number is float then if condition fails and goes to else part(i think this is what OP wants). so there will not be any problem in using it. check here
I understood the question in a way the he want's to make sure that the number has no decimals regardless of the data type. I'm not sure. Maybe I misunderstood that.
Yea i think what you understood was correct. What i am trying to say is using preg_ function for this small process is not good.we have other functions which can finish this work in less time.
|
1

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

Comments

1

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

2 Comments

I did test this for about 2 min's I might miss something. If I do please tell.
I'm not sure if this is what OP wants, but however, thanks for showing fmod() I didn't seen it before.
0
if (empty($calc - (int)$calc))
{
    return true; // is int
}else{
    return false; // is no int
}

Comments

0

Try this:

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

Comments

0

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

2 Comments

this too doesn't work.check here It makes 123 as 124 which is wrong.
your editted code also will not work in case of OP's situation.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.