Current Location: Home> Latest Articles> How to use the PHP floor() function to round down the floating value

How to use the PHP floor() function to round down the floating value

gitbox 2025-05-27

Basic syntax

 floor(float $value): float
  • $value is a floating point number that needs to be rounded downwards.

  • The return value is an integer rounded downward, and is of type a floating point number.


Sample code

 <?php
// Round down example
$num1 = 5.9;
$num2 = -3.2;

echo floor($num1); // Output 5
echo "\n";
echo floor($num2); // Output -4
?>

Application scenarios

  1. Calculation pagination <br> When you need to calculate the total number of pages based on the total number and the number of pages per page, you can use floor() to round down first and then add one to ensure that the page is correct.

  2. Price calculation <br> Sometimes the price is rounded downward to prevent situations that exceed the budget.

  3. Coordinate processing <br> When processing map coordinates or graphics, floor() is commonly used to round down the floating point coordinates to pixel points.


Things to note

  • floor() is different from ceil() (rounded up) and round() (rounded) and choose a suitable function to avoid calculation errors.

  • When there is a negative number, floor() will round in the direction of a smaller integer, such as -3.2 will be taken to -4 .


References

 <?php
// More About floor() Please refer to the usage of the function:
// https://gitbox.net/manual/en/function.floor.php
?>