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.
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:
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:
|
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
|
1 2 3 4 5 6 7 8 9 |
<?php echo(round(0.70) . "<br>"); echo(round(1.70) . "<br>"); echo(round(1.40) . "<br>"); echo(round(1.50) . "<br>"); echo(round(0.47) . "<br>"); echo(round(-5.40) . "<br>"); echo(round(-5.60)); ?> |
|
1 2 3 4 5 6 7 |
1 2 1 2 0 -5 -6 |
Example:2
Round numbers to two decimals:
|
1 2 3 4 5 |
<?php echo(round(5.867314,2) . "<br>"); echo(round(8.032,2) . "<br>"); echo(round(6.564,2)); ?> |
|
1 2 3 |
5.87 8.03 6.56 |
Example:3
Round numbers using the constants:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php echo(round(3.5,0,PHP_ROUND_HALF_EVEN) . "<br>"); echo(round(-3.5,0,PHP_ROUND_HALF_EVEN) . "<br>"); echo(round(3.5,0,PHP_ROUND_HALF_ODD) . "<br>"); echo(round(-3.5,0,PHP_ROUND_HALF_ODD). "<br>"); echo(round(3.5,0,PHP_ROUND_HALF_UP) . "<br>"); echo(round(-3.5,0,PHP_ROUND_HALF_UP) . "<br>"); echo(round(3.5,0,PHP_ROUND_HALF_DOWN) . "<br>"); echo(round(-3.5,0,PHP_ROUND_HALF_DOWN)); ?> |
|
1 2 3 4 5 6 7 8 |
4 -4 3 -3 4 -4 3 -3 |
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.