在 PHP 中对浮点数进行取整

Sheeraz Gul 2022年7月18日
在 PHP 中对浮点数进行取整

在 PHP 中,round() 函数对浮点数进行舍入。本教程演示如何使用 PHP 的 round() 函数。

在 PHP 中舍入浮点数

round() 函数对 PHP 中的浮点数进行舍入。我们可以使用它来定义一个特定的精度值,该值将四舍五入,保持精度值在视图中。

此精度值可以为零或负数。该函数有三个参数;这个函数的语法是:

float round(number, precision, mode);

在哪里:

  1. number 是我们要四舍五入的浮点数。

  2. precision 是一个可选参数,它决定要舍入到的小数位数。默认的 precision 值为零。

  3. mode 是一个可选参数,用于指定一个常数,指定舍入模式。模式常量有四种类型:

    3.1. PHP_ROUND_HALF_UP:此模式将告诉方法将数字从零舍入。
    3.2. PHP_ROUND_HALF_DOWN:此模式将告诉方法将数字舍入为零。
    3.3. PHP_ROUND_HALF_EVEN:此模式将告诉方法将数字舍入到最接近的偶数值。
    3.4. PHP_ROUND_HALF_ODD:此模式将告诉方法将数字舍入到最接近的奇数值。

此方法的返回值是四舍五入的数字。让我们试试这个方法的一个例子。

<?php
$RoundValue1=round(10.3);
$RoundValue2=round(12.5);
$RoundValue3=round(13.6);
$RoundValue4=round(14.6,0);
$RoundValue5=round(6.97553,2);
$RoundValue6=round(7.76521,-3);
$RoundValue7=round(8.063,2);
$RoundValue8=round(9.076,2);
$RoundValue9=round(6.97553, PHP_ROUND_HALF_UP);
$RoundValue10=round(6.97553, PHP_ROUND_HALF_DOWN);
$RoundValue11=round(6.97553, PHP_ROUND_HALF_EVEN);
$RoundValue12=round(6.97553, PHP_ROUND_HALF_ODD) ;


echo "The Round Value for (10.3)  is : ". $RoundValue1 . "<br /><br />" ;
echo "The Round Value for (12.5)  is : ". $RoundValue2 . "<br /><br />" ;
echo "The Round Value for (13,6)  is : ". $RoundValue3 . "<br /><br />" ;
echo "The Round Value for (14.6,0)  is : ". $RoundValue4 . "<br /><br />" ;
echo "The Round Value for (6.97553,2)  is : ". $RoundValue5 . "<br /><br />" ;
echo "The Round Value for (7.76521,-3) is : ". $RoundValue6 . "<br /><br />" ;
echo "The Round Value for (8.063,2)  is : ". $RoundValue7 . "<br /><br />" ;
echo "The Round Value for (9.076,2)  is : ". $RoundValue8 . "<br /><br />" ;
echo "The Round Value for (6.97553,2, PHP_ROUND_HALF_UP)  is : ". $RoundValue9 . "<br /><br />" ;
echo "The Round Value for (6.97553,2, PHP_ROUND_HALF_DOWN) is : ". $RoundValue10 . "<br /><br />" ;
echo "The Round Value for (6.97553,2, PHP_ROUND_HALF_EVEN)  is : ". $RoundValue11 . "<br /><br />" ;
echo "The Round Value for (6.97553,2, PHP_ROUND_HALF_ODD)  is : ". $RoundValue12 . "<br /><br />" ;
?>

上面的代码显示了 round() 方法的 12 个不同版本的用法。查看输出:

The Round Value for (10.3) is : 10

The Round Value for (12.5) is : 13

The Round Value for (13,6) is : 14

The Round Value for (14.6,0) is : 15

The Round Value for (6.97553,2) is : 6.98

The Round Value for (7.76521,-3) is : 0

The Round Value for (8.063,2) is : 8.06

The Round Value for (9.076,2) is : 9.08

The Round Value for (6.97553,2, PHP_ROUND_HALF_UP) is : 7

The Round Value for (6.97553,2, PHP_ROUND_HALF_DOWN) is : 6.98

The Round Value for (6.97553,2, PHP_ROUND_HALF_EVEN) is : 6.976

The Round Value for (6.97553,2, PHP_ROUND_HALF_ODD) is : 6.9755
作者: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

相关文章 - PHP Number