合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
[TOC] ## **数学函数** * [bcadd](https://www.php.net/manual/zh/function.bcadd.php "bcadd")(string`$num1`,string`$num2`,?int`$scale`\=**`null`**):两个任意精度数字的**加法**计算 * [bcsub](https://www.php.net/manual/zh/function.bcsub.php "bcsub")(string`$num1`,string`$num2`,?int`$scale`\=**`null`**):两个任意精度数字的**减法** * [bcmul](https://www.php.net/manual/zh/function.bcmul.php "bcmul")(string`$num1`,string`$num2`,?int`$scale`\=**`null`**):两个任意精度数字**乘法**计算 * [bcdiv](https://www.php.net/manual/zh/function.bcdiv.php "bcdiv")(string`$num1被除数`,string`$num2除数且不能为0否则返回null`,?int`$scale`\=**`null 保留的小数点`**):两个任意精度的数字**除法**计算 * [bccomp](https://www.php.net/manual/zh/function.bccomp.php "bccomp")(string`$num1`,string`$num2`,?int`$scale`\=**`null`**):比较两个任意精度的数字 。 两个数相等时返回 0;`num1`比`num2`大时返回 1; 其他则返回 -1 * [bcmod](https://www.php.net/manual/zh/function.bcmod.php "bcmod")(string`$num1`,string`$num2`,?int`$scale`\=**`null`**):任意精度数字取模 * [bcpow](https://www.php.net/manual/zh/function.bcpow.php "bcpow")(string`$num`,string`$exponent`,?int`$scale`\=**`null`**):任意精度数字的乘方 * [bcpowmod](https://www.php.net/manual/zh/function.bcpowmod.php "bcpowmod"):将一个任意精度数提高到另一个,并按指定的modu降低 * [bcscale](https://www.php.net/manual/zh/function.bcscale.php "bcscale")(int`$scale`):设置/获取所有 bc math 函数的默认小数点保留位数 * [bcsqrt](https://www.php.net/manual/zh/function.bcsqrt.php "bcsqrt")(string`$num`,?int`$scale`\=**`null`**):任意精度数字的二次方根 * sqrt(被开方的数)求平方根 ``` echo sqrt(9); //3 ``` * abs($number)求绝对值 ``` $abs = abs(-4.2); //4.2 ``` * ceil()进一法取整 ``` echo ceil(9.999); // 10 ``` * floor()舍去法取整 ``` echo floor(9.999); // 9 ``` * fmod()浮点数取余 ``` $x = 5.7;$y = 1.3; $r = fmod($x, $y); // $r 等于0.5, 因为4 * 1.3 + 0.5 = 5.7 ``` * pow(基础数, n次方)返回数的n次方 ``` echo pow(-1, 20); // 1 ``` * round(一个数值, 保留小数点后多少位 默认为0)浮点数四舍五入 ``` echo round(1.95583, 2);// 1.96 ``` * max()求最大值 ``` echo max(1, 3, 5, 6, 7); // 7 echo max(array(2, 4, 5)); // 5 ``` * min()求最小值 ``` echo max(1, 3, 5, 6, 7); // 1 echo max(array(2, 4, 5)); // 2 ``` * mt_rand()更好的随机数 ``` echo mt_rand(0,9);//n ``` * rand()随机数 * pi(void)获取圆周率值 ``` echo pi(); // 3.1415926535898 ``` 多个数相加 ``` /** * 多个数字相加 * @param [type] $decimal_places 保留小数点位数 * @param number|array $number 计算的数字 * @return 返回相加后的结果 */ function mathAdd($decimal_places,...$number){ $temp = 0; $total = 0; if (is_array($number[0])) { foreach($number[0] as $p){ $total = bcadd($p,$temp,$decimal_places); $temp = $total; } }else{ foreach($number as $p){ $total = bcadd($p,$temp,$decimal_places); $temp = $total; } } return $total; } // $total=mathAdd(6,1,2,3); $total=mathAdd(6,[1,2,3]); echo $total; //6.000000 ``` ## **intval**() 获取变量的整数值