(PHP 8 >= 8.4.0)
bcround — 对任意精度数字进行四舍五入
返回num
四舍五入到指定precision
(小数点后位数)的值。precision
也可以是负数或零(默认值)。
num
要四舍五入的值。
precision
可选的小数位数,用于四舍五入。
如果precision
为正数,则num
将四舍五入到小数点后precision
位有效数字。
如果precision
为负数,则num
将四舍五入到小数点前precision
位有效数字,即四舍五入到pow(10, -$precision)
的最近倍数,例如,对于precision
为-1,num
将四舍五入到十位数,对于precision
为-2,则四舍五入到百位数,等等。
mode
返回一个数值字符串,表示num
四舍五入到给定精度后的值。
示例 #1 bcround() 示例
<?php
var_dump(bcround('3.4'));
var_dump(bcround('3.5'));
var_dump(bcround('3.6'));
var_dump(bcround('3.6', 0));
var_dump(bcround('5.045', 2));
var_dump(bcround('5.055', 2));
var_dump(bcround('345', -2));
var_dump(bcround('345', -3));
var_dump(bcround('678', -2));
var_dump(bcround('678', -3));
?>
以上示例将输出
string(1) "3" string(1) "4" string(1) "4" string(1) "4" string(4) "5.05" string(4) "5.06" string(3) "300" string(1) "0" string(3) "700" string(4) "1000"
示例 #2 使用bcround() 和不同precision
值的示例
<?php
$number = '123.45';
var_dump(bcround($number, 3));
var_dump(bcround($number, 2));
var_dump(bcround($number, 1));
var_dump(bcround($number, 0));
var_dump(bcround($number, -1));
var_dump(bcround($number, -2));
var_dump(bcround($number, -3));
?>
以上示例将输出
string(7) "123.450" string(6) "123.45" string(5) "123.5" string(3) "123" string(3) "120" string(3) "100" string(1) "0"
示例 #3 使用bcround() 和不同mode
值的示例
<?php
echo '舍入模式与9.5' . PHP_EOL;
var_dump(bcround('9.5', 0, RoundingMode::HalfAwayFromZero));
var_dump(bcround('9.5', 0, RoundingMode::HalfTowardsZero));
var_dump(bcround('9.5', 0, RoundingMode::HalfEven));
var_dump(bcround('9.5', 0, RoundingMode::HalfOdd));
var_dump(bcround('9.5', 0, RoundingMode::TowardsZero));
var_dump(bcround('9.5', 0, RoundingMode::AwayFromZero));
var_dump(bcround('9.5', 0, RoundingMode::NegativeInfinity));
var_dump(bcround('9.5', 0, RoundingMode::PositiveInfinity));
echo PHP_EOL;
echo '舍入模式与8.5' . PHP_EOL;
var_dump(bcround('8.5', 0, RoundingMode::HalfAwayFromZero));
var_dump(bcround('8.5', 0, RoundingMode::HalfTowardsZero));
var_dump(bcround('8.5', 0, RoundingMode::HalfEven));
var_dump(bcround('8.5', 0, RoundingMode::HalfOdd));
var_dump(bcround('8.5', 0, RoundingMode::TowardsZero));
var_dump(bcround('8.5', 0, RoundingMode::AwayFromZero));
var_dump(bcround('8.5', 0, RoundingMode::NegativeInfinity));
var_dump(bcround('8.5', 0, RoundingMode::PositiveInfinity));
?>
以上示例将输出
Rounding modes with 9.5 string(2) "10" string(1) "9" string(2) "10" string(1) "9" string(1) "9" string(2) "10" string(1) "9" string(2) "10" Rounding modes with 8.5 string(1) "9" string(1) "8" string(1) "8" string(1) "9" string(1) "8" string(1) "9" string(1) "8" string(1) "9"
示例 #4 使用 bcround() 函数及不同 mode
值指定 precision
精度的示例
<?php
echo '使用 RoundingMode::HalfAwayFromZero 进行小数点后一位精度舍入' . PHP_EOL;
var_dump(bcround( 1.55, 1, RoundingMode::HalfAwayFromZero));
var_dump(bcround(-1.55, 1, RoundingMode::HalfAwayFromZero));
echo PHP_EOL;
echo '使用 RoundingMode::HalfTowardsZero 进行小数点后一位精度舍入' . PHP_EOL;
var_dump(bcround( 1.55, 1, RoundingMode::HalfTowardsZero));
var_dump(bcround(-1.55, 1, RoundingMode::HalfTowardsZero));
echo PHP_EOL;
echo '使用 RoundingMode::HalfEven 进行小数点后一位精度舍入' . PHP_EOL;
var_dump(bcround( 1.55, 1, RoundingMode::HalfEven));
var_dump(bcround(-1.55, 1, RoundingMode::HalfEven));
echo PHP_EOL;
echo '使用 RoundingMode::HalfOdd 进行小数点后一位精度舍入' . PHP_EOL;
var_dump(bcround( 1.55, 1, RoundingMode::HalfOdd));
var_dump(bcround(-1.55, 1, RoundingMode::HalfOdd));
echo PHP_EOL;
echo '使用 RoundingMode::TowardsZero 进行小数点后一位精度舍入' . PHP_EOL;
var_dump(bcround( 1.55, 1, RoundingMode::TowardsZero));
var_dump(bcround(-1.55, 1, RoundingMode::TowardsZero));
echo PHP_EOL;
echo '使用 RoundingMode::AwayFromZero 进行小数点后一位精度舍入' . PHP_EOL;
var_dump(bcround( 1.55, 1, RoundingMode::AwayFromZero));
var_dump(bcround(-1.55, 1, RoundingMode::AwayFromZero));
echo PHP_EOL;
echo '使用 RoundingMode::NegativeInfinity 进行小数点后一位精度舍入' . PHP_EOL;
var_dump(bcround( 1.55, 1, RoundingMode::NegativeInfinity));
var_dump(bcround(-1.55, 1, RoundingMode::NegativeInfinity));
echo PHP_EOL;
echo '使用 RoundingMode::PositiveInfinity 进行小数点后一位精度舍入' . PHP_EOL;
var_dump(bcround( 1.55, 1, RoundingMode::PositiveInfinity));
var_dump(bcround(-1.55, 1, RoundingMode::PositiveInfinity));
?>
以上示例将输出
Using RoundingMode::HalfAwayFromZero with 1 decimal digit precision string(3) "1.6" string(4) "-1.6" Using RoundingMode::HalfTowardsZero with 1 decimal digit precision string(3) "1.5" string(4) "-1.5" Using RoundingMode::HalfEven with 1 decimal digit precision string(3) "1.6" string(4) "-1.6" Using RoundingMode::HalfOdd with 1 decimal digit precision string(3) "1.5" string(4) "-1.5" Using RoundingMode::TowardsZero with 1 decimal digit precision string(3) "1.5" string(4) "-1.5" Using RoundingMode::AwayFromZero with 1 decimal digit precision string(3) "1.6" string(4) "-1.6" Using RoundingMode::NegativeInfinity with 1 decimal digit precision string(3) "1.5" string(4) "-1.6" Using RoundingMode::PositiveInfinity with 1 decimal digit precision string(3) "1.6" string(4) "-1.5"