Up and Down Round values in PHP: ceil, floor and round functions

Table of Contents

Tutorial and examples of the use of the ceil, floor, and round functions of PHP used in operations that involve the rounding of numeric values ​​in programming.

 

One of the challenges of working with numerical values ​​in programming is the question of rounding values ​​in operations cases that result in fractional values. The math is not exact, or instead, it is accurate, but we limit the fractional results to x decimal places, and there will always be something left, and the problem is that we need to say what to do with this leftover.

 

Make your WordPress site’s Load Blazing Fast Just by moving to Nestify. Migrate your WooCommerce Store or WordPress Website NOW.

In PHP, there are three functions for the treatment of fractional values. You can round up, always; round down (truncate), permanently, or let PHP do that. In the latter case, if the overshoot is greater than 5, it will be rounded up. Otherwise, the value will be truncated.

 

Round up – Function Ceil

 

The ceil () function is used to round the fractional value up. Here are some examples:

<? php

echo ceil (7.3); // The result will be rounded to 8

echo ceil (2,999); // The result will be rounded to 3

echo ceil (-6.12); // The result will be rounded to -6

?>

The values ​​with fractional results were rounded up in all of the above examples. This is particularly useful for routines that generate result pagination or other similar acts.

 

Round Down – Floor Function

 

The floor () function works in the ceil inverse, which rounds the fractional values ​​down. If we take the same example previously described, we will have the following results:

 

<? php

echo floor (7.3); // The result will be rounded to 7

echo floor (2,999); // The result will be rounded to 2

echo floor (-6.12); // Will the result be rounded to -7

?

 

Rounding down values ​​is useful when only the whole part interests us, though you cannot use int since both ceil, floor, and round work with type float.

 

Automatic rounding – Round function

 

The round () function does the work of ceil and floor. However, it is flexible and can round the values ​​up and down. See the same example being applied with the round () function:

 

<? php

echo round (7.3); // The result will be rounded to 7

echo round (2,999); // The result will be rounded to 3

echo round (-6.12); // The result will be rounded to -6

?>

 

As we can see, the values ​​are rounded up when the fractional part is greater than or equal to 5 and will round down when the fractional part is less than 5.

 

In most cases, this is the standard procedure for rounding values ​​in all programming languages ​​and most business operations.

 

Fully Managed WordPress Hosting

Nestify’s AWS powered dedicated CPU servers keep your sites fast, secure, and always up to date.

Want faster WordPress?

WordPress Speed Optimization

Try our AWS powered WordPress hosting for free and see the difference for yourself.

No Credit Card Required.

Whitelabel Web Hosting Portal Demo

Launching WordPress on AWS takes just one minute with Nestify.

Launching WooCommerce on AWS takes just one minute with Nestify.