php常用数学函数bcadd,bcsub,bcmul,bcdiv等详解

PHP piniu 1114浏览 0评论

1. bcadd – 将两个任意精度数字相加

string bcsub ( string $left_operand , string $right_operand [, int $scale = int ] )

将$left_operand与$right_operand求和,并返回字符串类型的结果。$scale为可选参数,用于设置结果中小数点后的小数位数。默认为 0

例子:

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

2.bcsub – 两个任意精度数字的减法

string bcsub ( string $left_operand , string $right_operand [, int $scale = int ] )

$left_operand 减去$right_operand 并返回字符串类型的结果。 $scale为可选参数,用于设置结果中小数点后的小数位数。默认为 0

例子:

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

3.bcmul – 两个任意精度数字乘法计算

string bcmul ( string $left_operand , string $right_operand [, int $scale = int ] )

$left_operand乘以$right_operand 并返回字符串类型的结果。$scale为可选参数,用于设置结果中小数点后的小数位数。默认为 0

例子:

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

4.bcdiv – 两个任意精度的数字除法计算

string bcdiv ( string $left_operand , string $right_operand [, int $scale = int ] )

将$left_operand 除以 $right_operand 并返回字符串类型的结果。(注:如果右操作数是0,结果为null) $scale为可选参数,用于设置结果中小数点后的小数位数。默认为 0

例子:

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

5.bcmod – 对一个任意精度数字取模

string bcmod ( string $left_operand , string $modulus )

对$left_operand使用$modulus 取模, 返回字符串类型的取模后结果,如果$modulus为0,则返回null

例子:

<?php
echo bcmod('4', '2'); // 0
echo bcmod('2', '4'); // 2

6.bcpow – 任意精度数字的乘方

string bcpow ( string $left_operand , string $right_operand [, int $scale ] )

求$left_operand的$right_operand次方运算。$scale为可选参数,用于设置结果中小数点后的小数位数。默认为 0

例子:

<?php
 
echo bcpow('4.2', '3', 2); // 74.08
echo bcpow('5', '2', 2);     // 结果是 "25", 不是 "25.00"

7.bcsqrt – 任意精度数字的二次方根
string bcsqrt ( string $operand [, int $scale ] )
返回$operand的二次方根,并返回字符串类型的结果。如果$operand是负数,则返回null。 可选的 scale参数设置结果中小数点后的位数。
例子:

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

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • * 昵称:
  • * 邮箱: