Whenever there is a need to round up or round down the given floating-point number to the nearest integer, we make use of a function called round function in PHP. It takes three parameters, namely number, precision and mode. Where number is the floating-point number that has been rounded up or rounded down to the nearest integer. Precision signifies the number of decimal digits that are to be rounded to, and this parameter is optional, and mode is a constant that specifies the rounding mode, and this parameter is also optional whose values can be either PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN or PHP_ROUND_HALF_ODD.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax to declare a round function in PHP:
round(number, precision, mode)
Where,
Given below are the examples of PHP round:
PHP program to illustrate the working of round function to round off the given floating-point integers to the nearest integers up to three decimal values.
Code:
<html> <body> <?php #round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called a $a = round(6.7654,3); #round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called b $b = round(2.98765,3); #round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called c $c = round(5.87654,3); #The result after using the round function is displayed as the output on the screen echo("The value after the given floating point number is rounded to the nearest integer is: "); echo $a; echo "<br>"; echo("The value after the given floating point number is rounded to the nearest integer is: "); echo $b; echo "<br>"; echo("The value after the given floating point number is rounded to the nearest integer is: "); echo $c; echo "<br>"; ?> </body> </html>
Output:
In the above program, the round function is used to round the given floating-point integers to the nearest integer up to three decimal values and is stored in the variables called a, b and c. Then, the result after rounding the given floating-point integers to the nearest integers2 up to decimal values specified in the round function is displayed as the output on the screen.
PHP program to illustrate the working of round function to round off the given floating-point integers to the nearest integers up to one decimal value.
Code:
<html> <body> <?php #round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called a $a = round(8.37465,1); #round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called b $b = round(1.09456,1); #round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called c $c = round(3.87654,1); #The result after using the round function is displayed as the output on the screen echo("The value after the given floating point number is rounded to the nearest integer is: "); echo $a; echo "<br>"; echo("The value after the given floating point number is rounded to the nearest integer is: "); echo $b; echo "<br>"; echo("The value after the given floating point number is rounded to the nearest integer is: "); echo $c; echo "<br>"; ?> </body> </html>
Output:
In the above program, the round function is used to round the given floating-point integers to the nearest integer up to three decimal values and is stored in the variables called a, b and c. Then, the result after rounding the given floating-point integers to the nearest integers2 up to decimal values specified in the round function is displayed as the output on the screen.
PHP program to illustrate the working of round function to round off the given floating-point integers to the nearest integers up to two decimal values.
Code:
<html> <body> <?php #round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called a $a = round(4.273645,2); #round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called b $b = round(7.45567,2); #round function is used on the given floating point number to round to up to three decimal values and is stored in a variable called c $c = round(9.3778335,2); #The result after using the round function is displayed as the output on the screen echo("The value after the given floating point number is rounded to the nearest integer is: "); echo $a; echo "<br>"; echo("The value after the given floating point number is rounded to the nearest integer is: "); echo $b; echo "<br>"; echo("The value after the given floating point number is rounded to the nearest integer is: "); echo $c; echo "<br>"; ?> </body> </html>
Output:
In the above program, the round function is used to round the given floating-point integers to the nearest integer up to three decimal values and is stored in the variables called a, b and c. Then, the result after rounding the given floating-point integers to the nearest integers2 up to decimal values specified in the round function is displayed as the output on the screen.
The above is the detailed content of PHP round. For more information, please follow other related articles on the PHP Chinese website!