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.

php Courses
php Courses
php Courses
How to Use PHP's ceil() Function to Round Numbers Up

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: 4 In 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: -5 Here, $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: 10 In 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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentPHPCode Examplemath functionceilrounding up
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.