Backend Development 4 min read

Using PHP ceil() Function to Round Numbers Upward

This article explains PHP's ceil() function, detailing its syntax, behavior for floating‑point and integer values, and provides three practical code examples demonstrating how to effectively round numbers upward in a variety of scenarios.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP ceil() Function to Round Numbers Upward

In PHP, the ceil() function is used to round a number up to the smallest integer greater than or equal to the given value. It is useful when you need to ensure a floating‑point number is rounded upward.

The syntax of the ceil() function is:

float ceil(float $number)

Here, $number is the value to be rounded up. The function returns the smallest integer that is greater than or equal to $number .

Below are three concrete code examples that illustrate how to use the ceil() function.

Example 1:

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

The output of this 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 this code is:

Rounded up result: -5

Here, the negative floating‑point number $number is -5.7. The ceil() function rounds it up to -5, which is the smallest integer greater than or equal to -5.7.

Example 3:

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

The output of this 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 practical tool for rounding numbers upward, whether dealing with floating‑point numbers or integers. These examples clearly demonstrate its usage and effects, making it valuable for mathematical calculations and data processing tasks.

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.