The round()
function in PHP is used to round a floating-point number to the nearest integer or to a specified number of decimal places.
🔹 Syntax:
round(float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): float
🔹 Parameters:
$num
: The number you want to round.$precision
(optional): Number of decimal digits to round to. Default is 0.$mode
(optional): Specifies how rounding should behave (default isPHP_ROUND_HALF_UP
).
🔹 Common Modes:
PHP_ROUND_HALF_UP
(default): Rounds halves up (e.g., 1.5 becomes 2)PHP_ROUND_HALF_DOWN
: Rounds halves down (e.g., 1.5 becomes 1)PHP_ROUND_HALF_EVEN
: Rounds to the nearest even number (e.g., 1.5 → 2, 2.5 → 2)PHP_ROUND_HALF_ODD
: Rounds to the nearest odd number (e.g., 1.5 → 1, 2.5 → 3)
🔹 Examples:
1. Basic Rounding:
echo round(3.4); // Output: 3
echo round(3.5); // Output: 4
2. Rounding with Precision:
echo round(3.456, 2); // Output: 3.46
echo round(3.456, 1); // Output: 3.5
3. Rounding with Modes:
echo round(2.5, 0, PHP_ROUND_HALF_UP); // Output: 3
echo round(2.5, 0, PHP_ROUND_HALF_DOWN); // Output: 2
echo round(2.5, 0, PHP_ROUND_HALF_EVEN); // Output: 2
echo round(2.5, 0, PHP_ROUND_HALF_ODD); // Output: 3
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.