Using PHP's ceil() Function to Round Numbers Upward

This article explains PHP's ceil() function, its syntax, and provides three practical code examples demonstrating how to round floating‑point and integer values upward to the smallest integer greater than or equal to the given number.

php Courses
php Courses
php Courses
Using PHP's ceil() Function to Round Numbers Upward

In PHP, the ceil() function is used for rounding a number up to the nearest integer that is greater than or equal to the original value.

Syntax of the ceil() function

float ceil(float $number)

Here, $number is the value to be rounded up, and ceil() returns the smallest integer that is not less than $number.

The following examples illustrate how to use the ceil() function in different scenarios.

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 set to 3.14 is rounded up using ceil(), resulting in 4, the smallest integer greater than or equal to 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, a negative floating‑point number $number set to -5.7 is rounded up, yielding -5, which is the smallest integer not less than -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, the integer $number is already 10, so ceil() returns the original value unchanged because it is already the smallest integer greater than or equal to itself.

In summary, the ceil() function in PHP is a useful tool for rounding up both floating‑point numbers and integers, and the provided examples clearly demonstrate its behavior and practical applications in mathematical calculations and data processing.

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.

BackendPHPRoundingmath functionsceil
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.