PHP intval vs floor

Is there any difference between floor()and intval()? Although both return the same results, are there any performance issues? Which of the two is faster? What proper php function will be used if I just want to return an integer value of only a decimal number?

The goal is to display 1999.99 through 1999, omitting the decimal value and returning only an integer.

$num = 1999.99;
$formattedNum = number_format($num)."<br>";
echo intval($num) . ' intval' . '<br />';
echo floor($num) . ' floor';
+4
source share
5 answers

Functions give different results with negative fractions.

echo floor(-0.1); // -1
echo intval(-0.1); // 0
+10
source

In docs ,

floor() - float, float , .

Intval() int.

+2

- casting: (int)$num.

+1
$v = 1999.99;
var_dump(
  intval($v),
  floor($v)
);

:

int(1999)
float(1999)

Docs:

http://php.net/manual/en/function.intval.php
http://php.net/manual/en/function.floor.php

0
source
$ratio_x = (0.70/100);

$computation =  $ratio_x * 8000;

$noDeci = floor(($ratio_x * 8000)*1)/1;

$intval = intval(($ratio_x * 8000)*1)/1;

$noDeci_hc = floor((int)56*1)/1;

echo $computation."|".$noDeci."|".$noDeci_hc."|".$intval;

CONCLUSION: 56 | 55 | 56 | 55

$ noDeci returns 55 ???

0
source

All Articles