How to Use PHP's ceil() Function to Round Numbers Up
This article explains PHP's ceil() function, shows its syntax, and provides three code examples demonstrating how to round floating‑point numbers and integers upward, including the resulting outputs and a brief discussion of each case.
In PHP, the ceil() function is used to round a number up to the smallest integer that is greater than or equal to the given floating‑point value.
Syntax of the ceil() Function
float ceil(float $number)The parameter $number represents the value that needs to be rounded up. The function returns the smallest integer that is greater than or equal to $number .
Using the ceil() Function
Example 1
$number = 3.14;
$ceilNumber = ceil($number);
echo "Rounded up result: " . $ceilNumber;The output of the above code is:
Rounded up result: 4In this example, a floating‑point number $number is set to 3.14 and ceil() rounds it up to 4, which is the smallest integer not less than 3.14.
Example 2
$number = -5.7;
$ceilNumber = ceil($number);
echo "Rounded up result: " . $ceilNumber;The output of the above code is:
Rounded up result: -5Here, $number is -5.7; ceil() rounds it up to -5, the smallest integer that is greater than or equal to -5.7.
Example 3
$number = 10;
$ceilNumber = ceil($number);
echo "Rounded up result: " . $ceilNumber;The output of the above code is:
Rounded up result: 10In this case, $number is already an integer (10). The ceil() function returns the original value unchanged because it is already the smallest integer not less than itself.
In summary, the ceil() function in PHP is a handy tool for rounding up both floating‑point numbers and integers. The provided examples clearly demonstrate its usage and effects, making it useful for mathematical calculations and data processing tasks.
Java learning materials
C language learning materials
Frontend learning materials
C++ learning materials
PHP learning materials
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.