Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
floor() function in PHP
The floor() function rounds a number down to the nearest integer. It always rounds towards negative infinity, which means positive decimals are rounded down and negative decimals are rounded further away from zero.
Syntax
floor(num)
Parameters
num − The number to round down
Return Value
The floor() function returns the value rounded down to the nearest integer as a float.
Example 1
Basic usage with positive decimal numbers −
<?php
echo(floor(0.10) . "<br>");
echo(floor(0.55) . "<br>");
echo(floor(9.99));
?>
0 0 9
Example 2
Using floor() with whole numbers −
<?php
echo(floor(9));
?>
9
Example 3
Behavior with negative numbers −
<?php
echo(floor(7.8) . "<br>");
echo(floor(-4.2) . "<br>");
echo(floor(-1.1));
?>
7 -5 -2
Key Points
For positive numbers,
floor()removes the decimal partFor negative numbers, it rounds further away from zero
The function returns a float value, not an integer
Whole numbers remain unchanged
Conclusion
The floor() function is useful for rounding numbers down to the nearest integer. Remember that it always rounds towards negative infinity, which affects how negative numbers are handled.
