Backend Development 4 min read

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

<code>float ceil(float $number)</code>

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:

<code>$number = 3.14;
$ceilNumber = ceil($number);
echo "Rounded up result: " . $ceilNumber;</code>

The output of the above code is:

<code>Rounded up result: 4</code>

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:

<code>$number = -5.7;
$ceilNumber = ceil($number);
echo "Rounded up result: " . $ceilNumber;</code>

The output of the above code is:

<code>Rounded up result: -5</code>

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:

<code>$number = 10;
$ceilNumber = ceil($number);
echo "Rounded up result: " . $ceilNumber;</code>

The output of the above code is:

<code>Rounded up result: 10</code>

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.

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

login 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.