PHP——bc函数及其应用详解

PHP——bc函数及其应用详解bcadd 两个任意精度数字的加法计算 PHP 4 PHP 5 PHP 7 PHP 8 bcadd string num1 string num2 int scale null string 注 对 num1

大家好,欢迎来到IT知识分享网。

bcadd —— 两个任意精度数字的加法计算 (PHP 4, PHP 5, PHP 7, PHP 8)

bcadd(string $num1, string $num2, ?int $scale = null): string

注:对 num1 和 num2 求和。

参数:

num1 — 左操作数,字符串类型。

num2 — 右操作数,字符串类型。

scale — 此可选参数用于设置结果中小数点后的小数位数。也可通过使用 bcscale() 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0。现在 scale 可以为 null。

返回值:以字符串返回两个操作数求和之后的结果。

范例:

<?php $a = '1.234'; $b = '5'; echo bcadd($a, $b); // 6 echo bcadd($a, $b, 4); // 6.2340 ?>

bcsub —— 两个任意精度数字的减法 (PHP 4, PHP 5, PHP 7, PHP 8)

bcsub(string $num1, string $num2, ?int $scale = null): string

注: num1 减去 num2 。

参数:

num1 — 左操作数,字符串类型。

num2 — 右操作数,字符串类型。

scale — 此可选参数用于设置结果中小数点后的小数位数。也可通过使用 bcscale() 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0。现在 scale 可以为 null。

返回值:以 string 类型返回减法之后的结果。

范例:

<?php $a = '1.234'; $b = '5'; echo bcsub($a, $b); // -3 echo bcsub($a, $b, 4); // -3.7660 ?>

bcmul —— 两个任意精度数字乘法计算 (PHP 4, PHP 5, PHP 7, PHP 8)

bcmul(string $num1, string $num2, ?int $scale = null): string

注: num1 乘以 num2 。

参数:

num1 — 左操作数,字符串类型。

num2 — 右操作数,字符串类型。

scale — 此可选参数用于设置结果中小数点后的小数位数。也可通过使用 bcscale() 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0。现在 scale 可以为 null。

返回值:以 string 类型返回减法之后的结果。

范例:

<?php echo bcmul('1.', '35', 3); // 47.161 echo bcmul('2', '4'); // 8 ?>

bcdiv —— 两个任意精度的数字除法计算 (PHP 4, PHP 5, PHP 7, PHP 8)

bcdiv(string $num1, string $num2, ?int $scale = null): string

注: num1 除以 num2 。

参数:

num1 — 左操作数,字符串类型。

num2 — 右操作数,字符串类型。

scale — 此可选参数用于设置结果中小数点后的小数位数。也可通过使用 bcscale() 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0。现在 scale 可以为 null。

返回值:以 string 类型返回减法之后的结果。

范例:

<?php echo bcdiv('105', '6.55957', 3); // 16.007 ?>

bccomp —— 比较两个任意精度的数字 (PHP 4, PHP 5, PHP 7, PHP 8)

bccomp(string $num1, string $num2, ?int $scale = null): int

注: 比较 num1num2, 并且返回整型数字的结果。

参数:

num1 — 左边的运算数,是一个字符串。

num2 — 右边的运算数,是一个字符串。

scale — 可选的 scale 参数被用作设置指示数字, 在使用来作比较的小数点部分。

返回值:两个数相等时返回 0; num1num2 小时返回 -1; 其他则返回 1。现在 scale 可以为 null。

范例:

<?php echo bccomp('1', '2') . "\n"; // -1 echo bccomp('1.00001', '1', 3); // 0 echo bccomp('1.00001', '1', 5); // 1 ?>

bcmod —— 任意精度数字取模 (PHP 4, PHP 5, PHP 7, PHP 8)

bcmod(string $num1, string $num2, ?int $scale = null): string

注: 对 num1 使用 num2 取模。 除非 num2 是零,否则结果必定和 num1 有相同的符号。

参数:

num1 — string 类型的被除数。

num2 — string 类型的除数。

scale — 现在 scale 可以为 null。

返回值:返回字符串类型取模后的结果,如果 num2 为 0 则返回 null。

范例:

<?php bcscale(0); echo bcmod( '5', '3'); // 2 echo bcmod( '5', '-3'); // 2 echo bcmod('-5', '3'); // -2 echo bcmod('-5', '-3'); // -2 bcscale(1); echo bcmod('5.7', '1.3'); // PHP 7.2.0 起是 0.5;之前是 0 ?>

bcpow—— 任意精度数字的乘方 (PHP 4, PHP 5, PHP 7, PHP 8)

bcpow(string $num, string $exponent, ?int $scale = null): string

注: num 的 exponent 次方运算。

参数:

num — string 类型的底数。

exponent — string 类型的指数。 如果指数不是整数,将被截断。 指数的有效范围取决于平台,但起码支持 – 到 的范围。

scale — 此可选参数用于设置结果中小数点后的小数位数。也可通过使用 bcscale() 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0。

返回值:返回字符串类型的结果。

范例:

<?php echo bcpow('4.2', '3', 2); // 74.08 echo bcpow('5', '2', 2); // prints "25", not "25.00" ?>

bcpowmod —— 先取次方然后取模。 (PHP 5, PHP 7, PHP 8)

bcpowmod( string $num, string $exponent, string $modulus, ?int $scale = null): string

注: 先取次方然后取模。

参数:

base — 左操作数。它是一个字符串类型的参数。

exponent — string 类型的指数。 指数的正确操作数。

modulus —string 类型的数。接受表示模数的操作数。

scale — 一个整数类型参数。它说明 ( base exponent %mod )结果中小数点后的位数。其默认值为 0。

返回值:该函数将结果作为字符串返回。或者,如果模数为 0 或指数为负,则返回 False。

范例:

<?php // 输入任意精度的数字 $base = "5"; $exponent = "7"; $mod = "7"; // 计算基数^指数 % mod $result = bcpowmod($base, $exponent, $mod); echo "无标度输出: ", $result; //无标度输出: 5 // 输入任意精度的数字 $base = "5"; $exponent = "7"; $mod = "7"; //比例值 4 $scale = 4; // 计算基数^指数 % mod $result = bcpowmod($base, $exponent, $mod, $scale); echo "带刻度的输出: ", $result; //带刻度的输出: 5.0000 ?>

bcscale —— 设置/获取所有 bc math 函数的默认小数点保留位数 (PHP 4, PHP 5, PHP 7, PHP 8)

bcscale(int $scale): int

设置所有 bc math 函数在未设定情况下的小数点保留位数。

bcscale(null $scale = null): int

注: 获取当前的小数点保留位数。

参数:

scale — 小数点保留位数。

返回值:设置的时候,返回之前的小数点保留位数。否则就是返回当前的位数。

范例:

<?php // 默认小数点位数: 3 bcscale(3); echo bcdiv('105', '6.55957'); // 16.007 // 不调用 bcscale() 也一样 echo bcdiv('105', '6.55957', 3); // 16.007 ?>

bcsqrt —— 任意精度数字的二次方根 (PHP 4, PHP 5, PHP 7, PHP 8)

bcsqrt(string $num, ?int $scale = null): string

注: 返回 num 的二次方根。

参数:

num — string 类型的操作数

scale — 此可选参数用于设置结果中小数点后的小数位数。也可通过使用 bcscale() 来设置全局默认的小数位数,用于所有函数。如果未设置,则默认为 0。

返回值:以 string 类型返回二次方根的结果,如果 num 是负数则返回 null。

范例:

<?php echo bcsqrt('2', 3); // 1.414 ?>

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/184887.html

(0)
上一篇 2025-08-03 09:45
下一篇 2025-08-03 10:00

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信