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

bcdivmod · Get the quotient and modulus of an arbitrary precision number

· 52CE 我爱测

bcdivmod — Get the quotient and modulus of an arbitrary precision number

bcdivmod

(PHP 8 >= 8.4.0)

bcdivmod — Get the quotient and modulus of an arbitrary precision number

说明

function bcdivmod(string $num1, string $num2, ?int $scale = null): array

Get the quotient and remainder of dividing num1 by num2.

参数

num1

被除数,字符串类型。

num2

除数,字符串类型。

scale

此参数用于设置结果小数点后的位数。 如果为 null,则默认为使用 bcscale() 设置的默认精度, 或者回退到 bcmath.scale INI 指令的值。

返回值

Returns an indexed array where the first element is the quotient as a string and the second element is the remainder as a string.

错误/异常

此函数在下列情况下抛出 ValueError:

  • num1 或 num2 不是格式正确的 BCMath 数字字符串。
  • scale 超出有效范围。

如果 num2 为 0,此函数会抛出 DivisionByZeroError 异常。

示例

示例 #1 bcdivmod() example

<?php
bcscale(0);

[$quot, $rem] = bcdivmod('5',  '3');
echo $quot; // 1
echo $rem;  // 2

[$quot, $rem] = bcdivmod('5',  '-3');
echo $quot; // -1
echo $rem;  // 2

[$quot, $rem] = bcdivmod('-5',  '3');
echo $quot; // -1
echo $rem;  // -2

[$quot, $rem] = bcdivmod('-5',  '-3');
echo $quot; // 1
echo $rem;  // -2
?>

示例 #2 bcdivmod() with decimals

<?php
[$quot, $rem] = bcdivmod('5.7', '1.3', 1);
echo $quot; // 4
echo $rem;  // 0.5
?>

参见

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

资料来源与适用范围

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

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

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