1
         CODE                            RESULTS

$a = floor(3.5);                         //3
$b = round(3.5, 0, PHP_ROUND_HALF_DOWN); //3
var_dump($a);                            //float(3)
var_dump($b);                            //float(3)
$c = gettype($a);                        //double
$d = gettype($b);                        //double

What are the difference.? when do I use floor() or round() for the number above.?

Mr world wide
  • 4,696
  • 7
  • 43
  • 97
ha_ryu
  • 568
  • 2
  • 7
  • 20
  • 1
    http://stackoverflow.com/a/580252/499581 – l'L'l Jan 13 '17 at 09:45
  • @l'L'l the given question is not .net like in your link – wake-0 Jan 13 '17 at 09:46
  • @KevinWallis: No, but the concept is likely the same. – l'L'l Jan 13 '17 at 09:47
  • Hi @ha_rya Its more of a mathematical operation. The behaviour of round() and floor() is same irrespective the language .net, python. – A user Jan 13 '17 at 09:48
  • does this question want to know what `floor` and `round` do? - or how the functions are implemented (technical difference)? – wake-0 Jan 13 '17 at 09:48
  • @I'L'l .NET and php have the same specification? – ha_ryu Jan 13 '17 at 09:49
  • There is no difference in this case, but the functions are different in other cases. For this case you have to specify the precision and the mode for the round() function. But for other numbers these functions act different, like floor(3.6) will still be 3, but round(3.6, 0, PHP_ROUND_HALF_DOWN) will be 4. – M. I. Jan 13 '17 at 09:49
  • @Kevin Wallis I have some php script that uses round but it does not seem to work well, but floor works, they have the same values, same type so I'm kind of confused. – ha_ryu Jan 13 '17 at 09:52

2 Answers2

10

floor() will simply drop decimal value and return only integer.

So floor(1.2) => 1 and floor(1.9) => 1.

Meanwhile round() will round number that has decimal value lower than 0.5 to lower int, and when more than 0.5 to higher int:

So round(1.2) => 1 but round(1.9) => 2

Also round() has more options, like precision and rounding options.


Example:

$nums = [-1.5, -1, -.8, -.4, 0, .4, .8, 1, 1.5];

echo "    \tround\tfloor\tceil" . PHP_EOL;
foreach ($nums as $a) {
    echo $a . ": \t" . round($a) . "\t" . floor($a) . "\t" . ceil($a) . PHP_EOL;
}

/*
        round   floor   ceil
-1.5:   -2      -2      -1
  -1:   -1      -1      -1
-0.8:   -1      -1      -0
-0.4:   -0      -1      -0
   0:    0       0       0
 0.4:    0       0       1
 0.8:    1       0       1
   1:    1       1       1
 1.5:    2       1       2

*/
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • ...although if dealing with negative numbers "higher" would mean lower I presume. (eg. `round(-1.9)`) would actually go lower not higher. – l'L'l Jan 13 '17 at 10:07
  • why floor(-0.01) = -1 but round(-0.01, 0, PHP_ROUND_HALF_DOWN) = -0 – gzhegow Jun 18 '22 at 22:59
  • @gzhegow Because your first output is wrong: https://onlinephp.io?s=s7EvyCjg5SpLLIpPKc0t0CjKL81L0dA10DMw1NS0BgA%2C&v=8.1.7 – Justinas Jun 20 '22 at 06:22
4

floor() will always remove the values after the decimal, and only rounds down. round() will round up if the value after the integer provided is equal or higher than .5, else it will round down.

Example 1: round(1.5) returns 2 while floor(1.5) returns 1.

Example 2: Both round(3.2) and floor(3.2) return 3.

Example 3: round(2.9) returns 3 while floor(2.9) returns 2.

mikespook
  • 768
  • 4
  • 13
UserName Name
  • 267
  • 2
  • 3