每当需要将给定的浮点数向上或向下舍入到最接近的整数时,我们都会使用 PHP 中称为舍入函数的函数。它需要三个参数,即数量、精度和模式。其中 number 是已向上舍入或向下舍入到最接近的整数的浮点数。 Precision表示要舍入的小数位数,该参数是可选的,mode是指定舍入模式的常量,该参数也是可选的,其值可以是PHP_ROUND_HALF_UP、PHP_ROUND_HALF_DOWN、PHP_ROUND_HALF_EVEN或PHP_ROUND_HALF_ODD。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
在 PHP 中声明舍入函数的语法:
round(number, precision, mode)
哪里,
以下是 PHP 回合的示例:
PHP 程序,用于说明舍入函数的工作原理,将给定的浮点整数四舍五入为最接近的整数,最多三位小数值。
代码:
<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>
输出:
在上面的程序中,round 函数用于将给定的浮点整数四舍五入到最接近的整数,最多三位小数,并存储在名为 a、b 和 c 的变量中。然后,将给定浮点整数四舍五入到最接近的整数2(最高可达舍入函数中指定的小数值)后的结果将作为输出显示在屏幕上。
PHP 程序,用于说明舍入函数的工作原理,将给定的浮点整数四舍五入到最接近的整数,最多一位小数值。
代码:
<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>
输出:
在上面的程序中,round 函数用于将给定的浮点整数四舍五入到最接近的整数,最多三位小数,并存储在名为 a、b 和 c 的变量中。然后,将给定浮点整数四舍五入到最接近的整数2(最高可达舍入函数中指定的小数值)后的结果将作为输出显示在屏幕上。
PHP 程序,用于说明舍入函数的工作原理,将给定的浮点整数四舍五入为最接近的整数,最多两位小数。
代码:
<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>
输出:
在上面的程序中,round 函数用于将给定的浮点整数四舍五入到最接近的整数,最多三位小数,并存储在名为 a、b 和 c 的变量中。然后,将给定浮点整数四舍五入到最接近的整数2(最高可达舍入函数中指定的小数值)后的结果将作为输出显示在屏幕上。
以上是PHP回合的详细内容。更多信息请关注PHP中文网其他相关文章!