How To Round UP PHP Values | PHP round() Function With Example
Summary: In this article, we will learn How to Round UP PHP values with the use of the PHP round() function with details. I hope you will enjoy it.
Introduction of Round up PHP | PHP round() Function
Suppose that when you want to deal with problems that have values consisting of a very high number of decimal digits like (151.77761527227). Therefore, we often come up with the problem of rounding them up. When you rounding them up manually can be a very time-consuming and erroneous practice. In place of that, you can use, an inbuilt function of PHP i.e round()
function.
The Round UP PHP i.e. round()
the inbuilt function can be used to round a floating-point number. you can be used to define a specific precision value that rounds numbers according to that precision value. Precision can be also zero or negative.
Basically, the round() function in PHP has three (3) parameters which are number, precision, and mode among that the latter 2(i.e. two) are optional parameters. The PHP round() function returns the rounded value of the argument passed.
Syntax:
Parameters: It will accept 3 parameters out of which one is mandatory and two are optional. All of these parameters are described as given below:
- $number: It’s the number that you would like to round.
- $precision: It’s an optional parameter. which specifies the number of decimal digits to round to. by the default value of this parameter is 0 (zero).
- $mode: It’s an optional parameter. which specifies a constant to specify the rounding mode. The constant will be one of the following:
- I). PHP_ROUND_HALF_EVEN: To round up the number described by parameter $number by precision specified by parameter $precision towards nearest even value.
- II). PHP_ROUND_HALF_ODD: To round up the number described by parameter $number by precision specified by parameter $precision towards nearest odd value.
- III). PHP_ROUND_HALF_UP: To round up the number described by parameter $number by precision specified by the parameter $precision away from zero.
- IV). PHP_ROUND_HALF_DOWN: To round up the number described by parameter $number by precision specified by parameter $precision towards zero.
PHP Rounding Example:
1 2 3 |
float ceil ( float value) float floor ( float value) float round ( float value [, int precision]) |
It is a standard situation that you just wish less accuracy than PHP offers you, during which case you would like to use one of PHP’s selection of rounding functions such as ceil(), floor(), and round().
Both ceil() & floor() takes just one (1) parameter – the number to round. Ceil() function is specified to take the number and round it to the nearest integer above its current value, whereas the floor() function rounds it to the nearest integer below its current value. there is the following example:
1 2 3 4 5 |
<?php $fewval = 6.9; $ceiled = ceil($fewval); $floored = floor($fewval); ?> |
$ceiled will be 7
and $floored will be 6
.
Example of PHP round() Function
Here we use the round(), the function which takes two parameters – the number to round, & the number of decimal places to round to. Furthermore, If variety is strictly halfway between two integers, round() will always round up.
Example:1
1 2 3 4 5 6 7 8 |
<?php echo ($a = round(7.9) . "<br>"); //8 echo ($b = round(7.5) . "<br>"); //8 echo ($c = round(7.4999) . "<br>"); //7 echo ($d = round(7.123456, 3) . "<br>"); // 7.123 echo ($e = round(7.12345, 4) . "<br>"); // 7.1235 echo ($f = round(1300 / 160) . "<br>"); //8 ?> |
Example:2
1 2 3 4 5 6 7 |
<?php echo ($a = round(0.80) . "<br>"); //1 echo ($b = round(0.818982) . "<br>"); //1 echo ($c = round(-5.30) . "<br>"); // -5 echo ($d = round(-5.70) . "<br>"); // -6 echo ($e = round(0.91878, 2)); // 0.92 ?> |
Example:3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php // round to nearest even value echo(round(8.5,0,PHP_ROUND_HALF_EVEN)); echo "\n"; // round to nearest odd value echo(round(8.5,0,PHP_ROUND_HALF_ODD)); echo "\n"; // round towards zero echo(round(8.5,0,PHP_ROUND_HALF_DOWN)); echo "\n"; // round away from zero echo(round(8.5,0,PHP_ROUND_HALF_UP)); ?> |
9
8
9
PHP Round Down
1 2 3 4 |
<?php // round down echo floor(3.5); // prints 3 ?> |
PHP Round UP
1 2 3 4 |
<?php // round up echo ceil(3.5); // prints 4 ?> |
Decimal Round Using PHP
1 2 3 4 5 6 |
<?php // using round() function we can roundoff float values in php $value = 111.25365; echo round($value, 2); //result 111.25 ?> |
Round UP PHP Related FAQs
How do I round up in PHP?
The round() is used to rounds a floating-point number. Note: When you want to round a number UP to the nearest integer, then look at the ceil() function. Note: Furthermore, When you want to round a number DOWN to the nearest integer, look at the floor() function.
Is number round in PHP?
The optional or alternative number of decimal digits to round to. If the precision is positive, then the num is rounded to precision significant digits after the decimal point.
PHP_ROUND_HALF_EVEN: Rounds num towards the nearest even value when it is halfway there, making both 1.5 and 2.5 into 2.
How do you round a number in HTML?
The Math. PHP round() method rounds a number to the nearest integer. Important Note: 3.48 will be rounded down (3), and 3.5 will be rounded up (3).
How do you round a decimal number?
You need to follow a few rules when rounding a decimal number. But generally, if the last digit is less than 5 (five), round the previous digit down. However, if it’s 5 or more then you should round the previous digit up. Therefore, if the number you are about to round is followed by 5, 6, 7, 8, 9 round the number up.
How do you round to the nearest integer?
If the digit in the tenths place is less than 5, then you need to round down function, which means the units digit remains the same; if the digit in the tenths place is 5 (five) or greater, then you need to round up, it means that you should increase the unit digit by one.
How do you round decimals in MySQL?
ROUND() Function in MySQL: It is used to round a number to a specified number of decimal places. Furthermore, if no specified number of decimal places is provided for round-off, then it rounds off the number to the nearest integer. X: The number to be rounded.