PHP Round() Function | How to round numbers in PHP?

Summary: in this article, you will learn about PHP Round() Function with example and How to round numbers in PHP? Let’s understand the Round() Function with an example.

PHP Round() Function Definition and Usage

  • While handling issues that have values consisting of a very high range of decimal digits like (231.76761527145) we often come up with a problem of rounding them up.
  • furthermore, rounding them up manually can be a lot of time-consuming and erroneous practice. In place of that, an inbuilt function of PHP i.e round() function can be used.
  • the round() function used to round a floating-point number.
  • It can be used to define a specific or particular precision value that rounds number according to that precision value. Precision can be also negative(-ve) or zero.
  • The round() function in PHP has three(3) parameters which are number, precision, and mode among which the latter two(2) are optional parameters.
  • Furthermore, when used the round() function in PHP, returns the rounded value of the argument passed.
Note: The round() function rounds a floating-point number.
To round a number UP to the nearest integer, look at the ceil() function.
To round a number DOWN to the nearest integer, look at the PHP floor() function.

Syntax:

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

Parameter Values

It accepts three parameters out of which one is compulsory and two are optional. All of these parameters are described below:

Parameter Description
$number Required. It is the number which you want to round.
$precision Optional. It specifies the number of decimal digits to round to. this parameter default value is zero.
$mode Optional. It specifies a constant to specify the rounding mode. therefore, the constant can be one of the following:

  • PHP_ROUND_HALF_EVEN: To round up the number specified by parameter $number by precision described by parameter $precision towards nearest even value.
  • PHP_ROUND_HALF_ODD: To round up the number specified by parameter $number by precision described by parameter $precision towards nearest odd value.
  • PHP_ROUND_HALF_UP: To round up the number specified by parameter $number by precision described by the parameter $precision away from zero.
  • PHP_ROUND_HALF_DOWN: To round up the number specified by parameter $number by precision described by parameter $precision towards zero.

Return Value: It will returns the rounded value.

Technical Details

Return Value: The rounded value
PHP Version: 4+
Return Type: Float
PHP Changelog: PHP 5.3: The mode parameter was added

PHP round() Function Example

Example:1

Example:2

Round numbers to two decimals:

Example:3

Round numbers using the constants:

PHP round() Function Related Question and Answer:

Question1: How do I round to 2 decimal places in PHP?

Answer: Use the PHP number_format() function. This will show or display exactly two digits after the decimal point. Advantage: If you wish to display two digits after a float value only and not for int, then use this


Question2: How do you round a number in PHP?

Answer: The round() function in PHP used to rounds a floating-point number. Note: To round a number UP to the nearest integer, look at the ceil() function. Tip: To round a number DOWN to the nearest integer, look at the floor() function.


Question3: What is the use of round () in PHP?

Answer: The round() function used to round a floating-point number. furthermore, It can be used to define a specific precision value that rounds number according to that precision value. Precision can be also negative(-ve) or zero.

Conclusion:

In this article, you have learned round() function with example. I hope you will enjoy it!. if have any query then contact on info@tutorialscan.com. I will try to resolve the problem.