PHP Round() not working properly [Solved] – round() Function

I saw many users facing the problem that PHP Round() not working properly but in this article, we will cover all the aspects of the PHP round() function with coding examples.

Introduction

In everyday life, When dealing with problems involving values with a large number of decimal digits, such as (151.3446579989535346547), we frequently run into the issue of rounding them up. Manually rounding things up can be both time-consuming and inaccurate. In its substitute, round(), a PHP built-in function, can be utilize.

To round a floating-point value in PHP, use the round() method. It can be use to provide a precision value, after which the number is round to that precision value. Precision may also be negative or zero.

Syntax and Examples PHP round()

The round() method in PHP takes three parameters: number, precision, and mode, the latter two of which are optional. The round value of the argument supplies and returned by the round() method.

Syntax:

float round($number, $precision, $mode);

As in the above syntax, It accepts three parameters, one of which is require and the other two are optional. The following are all of these parameters:

  1. $number: It is the number which you want to round.
  2. $precision: It is an optional parameter. It specifies the number of decimal digits to round to. The default value of this parameter is zero.
  3. $mode: It is an optional parameter. It specifies a constant to specify the rounding mode. The constant can be one of the following:
    • PHP_ROUND_HALF_UP: This mode instructs the rounding up of the number supplied by parameter $number by the precision defined by parameter $precision away from zero.
    • PHP_ROUND_HALF_DOWN: This mode instructs the rounding up of the number supplied by parameter $number by the precision defined by parameter $precision towars zero.
    • PHP_ROUND_HALF_EVEN: This mode instructs the rounding up of the number supplied by parameter $number by the precision defined by parameter $precision towards nearest even value.
    • PHP_ROUND_HALF_ODD: This mode instructs the rounding up of the number supplied by parameter $number by the precision defined by parameter $precision towards nearest odd value.

Now let’s see some examples to clarify the concept.

Example 01: When a parameter is passed with default precision i.e. ‘0’.

<?php
echo(round(0.50));
?>

Output:

PHP Round() simple

Example 02: When a parameter is passed with a specific precision value.

<?php
echo(round(0.70878454657657657, 5));
?>

Output:

PHP Round() with parameter

Example 03: When a negative value is passed as a parameter.

<?php
echo(round(-65.2324));
?>

Output:

negative value

Example 04: Passing parameters with mode.

<?php
// round to nearest even value
echo(round(10.5,0,PHP_ROUND_HALF_EVEN));
echo "<br>";
 
// round to nearest odd value
echo(round(10.5,0,PHP_ROUND_HALF_ODD));
echo "<br>";
 
// round towards zero
echo(round(10.5,0,PHP_ROUND_HALF_DOWN));
echo "<br>";
 
// round away from zero
echo(round(10.5,0,PHP_ROUND_HALF_UP));
?>

Output:

mode parameter

Conclusion

That’s all for this article we tried different examples of rounding off a digit to clarify the concept. During development, rounding values are useful for a variety of activities. However, be cautious when rounding figures and selecting the appropriate approach. I hope these examples helped you grasp the topic. if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn.

Suggested Articles:

  1. How to Use PHP in HTML Pages
  2. PHP echo shorthand with Code Example
  3. PHP echo Multiple Lines – How to write Multi-Line Strings in PHP?

Leave a Comment