大网数据广告活动
PHP 语言与运行手册

BcMath\Number::round · Rounds an arbitrary precision number

· 52CE 我爱测

BcMath\Number::round — Rounds an arbitrary precision number

BcMath\Number::round

(PHP 8 >= 8.4.0)

BcMath\Number::round — Rounds an arbitrary precision number

说明

public function BcMath\Number::round(int $precision = 0, RoundingMode $mode = RoundingMode::HalfAwayFromZero): BcMath\Number

Returns the rounded value of $this to specified precision (number of digits after the decimal point). precision can also be negative or zero (default).

参数

precision

可选的十进制小数点后数字的数目。

如果 precision 是正数,则 num 会四舍五入到小数点后 precision 位有效数字。

如果 precision 是负数,则 num 四舍五入到小数点前 precision 位有效数字。即 pow(10, -$precision) 最接近的倍数,例如,precision 为 -1,num 可以四舍五入到十位,precision 为 -2,num 可以四舍五入到百位,等等。

mode

Specifies the rounding mode. For more information about modes, see RoundingMode.

返回值

Returns the result as a new BcMath\Number object.

错误/异常

This method throws a ValueError if an invalid mode is specified.

示例

示例 #1 BcMath\Number::round() example

<?php
var_dump(
    new BcMath\Number('3.4')->round(),
    new BcMath\Number('3.5')->round(),
    new BcMath\Number('3.6')->round(),
    new BcMath\Number('3.6')->round(0),
    new BcMath\Number('5.045')->round(2),
    new BcMath\Number('5.055')->round(2),
    new BcMath\Number('345')->round(-2),
    new BcMath\Number('345')->round(-3),
    new BcMath\Number('678')->round(-2),
    new BcMath\Number('678')->round(-3),
);
?>

以上示例会输出:

object(BcMath\Number)#2 (2) {
  ["value"]=>
  string(1) "3"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#3 (2) {
  ["value"]=>
  string(1) "4"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(1) "4"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#5 (2) {
  ["value"]=>
  string(1) "4"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#6 (2) {
  ["value"]=>
  string(4) "5.05"
  ["scale"]=>
  int(2)
}
object(BcMath\Number)#7 (2) {
  ["value"]=>
  string(4) "5.06"
  ["scale"]=>
  int(2)
}
object(BcMath\Number)#8 (2) {
  ["value"]=>
  string(3) "300"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#9 (2) {
  ["value"]=>
  string(1) "0"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#10 (2) {
  ["value"]=>
  string(3) "700"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#11 (2) {
  ["value"]=>
  string(4) "1000"
  ["scale"]=>
  int(0)
}

示例 #2 Example of using BcMath\Number::round() with different precision values

<?php
$number = new BcMath\Number('123.45');

var_dump(
    $number->round(3),
    $number->round(2),
    $number->round(1),
    $number->round(0),
    $number->round(-1),
    $number->round(-2),
    $number->round(-3),
);
?>

以上示例会输出:

object(BcMath\Number)#2 (2) {
  ["value"]=>
  string(7) "123.450"
  ["scale"]=>
  int(3)
}
object(BcMath\Number)#3 (2) {
  ["value"]=>
  string(6) "123.45"
  ["scale"]=>
  int(2)
}
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(5) "123.5"
  ["scale"]=>
  int(1)
}
object(BcMath\Number)#5 (2) {
  ["value"]=>
  string(3) "123"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#6 (2) {
  ["value"]=>
  string(3) "120"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#7 (2) {
  ["value"]=>
  string(3) "100"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#8 (2) {
  ["value"]=>
  string(1) "0"
  ["scale"]=>
  int(0)
}

示例 #3 Example of using BcMath\Number::round() with different mode values

<?php
echo 'Rounding modes with 9.5' . PHP_EOL;
$number = new BcMath\Number('9.5');
var_dump(
    $number->round(0, RoundingMode::HalfAwayFromZero),
    $number->round(0, RoundingMode::HalfTowardsZero),
    $number->round(0, RoundingMode::HalfEven),
    $number->round(0, RoundingMode::HalfOdd),
    $number->round(0, RoundingMode::TowardsZero),
    $number->round(0, RoundingMode::AwayFromZero),
    $number->round(0, RoundingMode::NegativeInfinity),
    $number->round(0, RoundingMode::PositiveInfinity),
);

echo PHP_EOL;
echo 'Rounding modes with 8.5' . PHP_EOL;
$number = new BcMath\Number('8.5');
var_dump(
    $number->round(0, RoundingMode::HalfAwayFromZero),
    $number->round(0, RoundingMode::HalfTowardsZero),
    $number->round(0, RoundingMode::HalfEven),
    $number->round(0, RoundingMode::HalfOdd),
    $number->round(0, RoundingMode::TowardsZero),
    $number->round(0, RoundingMode::AwayFromZero),
    $number->round(0, RoundingMode::NegativeInfinity),
    $number->round(0, RoundingMode::PositiveInfinity),
);
?>

以上示例会输出:

Rounding modes with 9.5
object(BcMath\Number)#3 (2) {
  ["value"]=>
  string(2) "10"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#5 (2) {
  ["value"]=>
  string(1) "9"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#7 (2) {
  ["value"]=>
  string(2) "10"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#9 (2) {
  ["value"]=>
  string(1) "9"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#11 (2) {
  ["value"]=>
  string(1) "9"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#13 (2) {
  ["value"]=>
  string(2) "10"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#15 (2) {
  ["value"]=>
  string(1) "9"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#17 (2) {
  ["value"]=>
  string(2) "10"
  ["scale"]=>
  int(0)
}

Rounding modes with 8.5
object(BcMath\Number)#1 (2) {
  ["value"]=>
  string(1) "9"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#15 (2) {
  ["value"]=>
  string(1) "8"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#13 (2) {
  ["value"]=>
  string(1) "8"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#11 (2) {
  ["value"]=>
  string(1) "9"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#9 (2) {
  ["value"]=>
  string(1) "8"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#7 (2) {
  ["value"]=>
  string(1) "9"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#5 (2) {
  ["value"]=>
  string(1) "8"
  ["scale"]=>
  int(0)
}
object(BcMath\Number)#3 (2) {
  ["value"]=>
  string(1) "9"
  ["scale"]=>
  int(0)
}

示例 #4 Example of using BcMath\Number::round() with different mode values when specifying precision

<?php
$positive = new BcMath\Number('1.55');
$negative = new BcMath\Number('-1.55');

echo 'Using RoundingMode::HalfAwayFromZero with 1 decimal digit precision' . PHP_EOL;
var_dump(
    $positive->round(1, RoundingMode::HalfAwayFromZero),
    $negative->round(1, RoundingMode::HalfAwayFromZero),
);

echo PHP_EOL;
echo 'Using RoundingMode::HalfTowardsZero with 1 decimal digit precision' . PHP_EOL;
var_dump(
    $positive->round(1, RoundingMode::HalfTowardsZero),
    $negative->round(1, RoundingMode::HalfTowardsZero),
);

echo PHP_EOL;
echo 'Using RoundingMode::HalfEven with 1 decimal digit precision' . PHP_EOL;
var_dump(
    $positive->round(1, RoundingMode::HalfEven),
    $negative->round(1, RoundingMode::HalfEven),
);

echo PHP_EOL;
echo 'Using RoundingMode::HalfOdd with 1 decimal digit precision' . PHP_EOL;
var_dump(
    $positive->round(1, RoundingMode::HalfOdd),
    $negative->round(1, RoundingMode::HalfOdd),
);

echo PHP_EOL;
echo 'Using RoundingMode::TowardsZero with 1 decimal digit precision' . PHP_EOL;
var_dump(
    $positive->round(1, RoundingMode::TowardsZero),
    $negative->round(1, RoundingMode::TowardsZero),
);

echo PHP_EOL;
echo 'Using RoundingMode::AwayFromZero with 1 decimal digit precision' . PHP_EOL;
var_dump(
    $positive->round(1, RoundingMode::AwayFromZero),
    $negative->round(1, RoundingMode::AwayFromZero),
);

echo PHP_EOL;
echo 'Using RoundingMode::NegativeInfinity with 1 decimal digit precision' . PHP_EOL;
var_dump(
    $positive->round(1, RoundingMode::NegativeInfinity),
    $negative->round(1, RoundingMode::NegativeInfinity),
);

echo PHP_EOL;
echo 'Using RoundingMode::PositiveInfinity with 1 decimal digit precision' . PHP_EOL;
var_dump(
    $positive->round(1, RoundingMode::PositiveInfinity),
    $negative->round(1, RoundingMode::PositiveInfinity),
);
?>

以上示例会输出:

Using RoundingMode::HalfAwayFromZero with 1 decimal digit precision
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(3) "1.6"
  ["scale"]=>
  int(1)
}
object(BcMath\Number)#5 (2) {
  ["value"]=>
  string(4) "-1.6"
  ["scale"]=>
  int(1)
}

Using RoundingMode::HalfTowardsZero with 1 decimal digit precision
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(3) "1.5"
  ["scale"]=>
  int(1)
}
object(BcMath\Number)#6 (2) {
  ["value"]=>
  string(4) "-1.5"
  ["scale"]=>
  int(1)
}

Using RoundingMode::HalfEven with 1 decimal digit precision
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(3) "1.6"
  ["scale"]=>
  int(1)
}
object(BcMath\Number)#7 (2) {
  ["value"]=>
  string(4) "-1.6"
  ["scale"]=>
  int(1)
}

Using RoundingMode::HalfOdd with 1 decimal digit precision
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(3) "1.5"
  ["scale"]=>
  int(1)
}
object(BcMath\Number)#8 (2) {
  ["value"]=>
  string(4) "-1.5"
  ["scale"]=>
  int(1)
}

Using RoundingMode::TowardsZero with 1 decimal digit precision
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(3) "1.5"
  ["scale"]=>
  int(1)
}
object(BcMath\Number)#9 (2) {
  ["value"]=>
  string(4) "-1.5"
  ["scale"]=>
  int(1)
}

Using RoundingMode::AwayFromZero with 1 decimal digit precision
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(3) "1.6"
  ["scale"]=>
  int(1)
}
object(BcMath\Number)#10 (2) {
  ["value"]=>
  string(4) "-1.6"
  ["scale"]=>
  int(1)
}

Using RoundingMode::NegativeInfinity with 1 decimal digit precision
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(3) "1.5"
  ["scale"]=>
  int(1)
}
object(BcMath\Number)#11 (2) {
  ["value"]=>
  string(4) "-1.6"
  ["scale"]=>
  int(1)
}

Using RoundingMode::PositiveInfinity with 1 decimal digit precision
object(BcMath\Number)#4 (2) {
  ["value"]=>
  string(3) "1.6"
  ["scale"]=>
  int(1)
}
object(BcMath\Number)#12 (2) {
  ["value"]=>
  string(4) "-1.5"
  ["scale"]=>
  int(1)
}

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。