Home  >  Article  >  Backend Development  >  PHP round

PHP round

WBOY
WBOYOriginal
2024-08-29 13:10:161040browse

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,

  • the number is the floating-point number that is be 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.
  • 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.

Working of round Function in PHP

  • 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.
  • The round function in PHP takes three parameters, namely number, precision and mode.
  • The parameter number is the floating-point number that is be rounded up or rounded down to the nearest integer.
  • The parameter precision signifies the number of decimal digits to be rounded to, which is optional.
  • The parameter 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.

Examples of PHP round

Given below are the examples of PHP round:

Example #1

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:

PHP round

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.

Example #2

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:

PHP round

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.

Example #3

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:

PHP round

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP Testing FrameworkNext article:PHP Testing Framework