Skip to main content

任意精度的常用数学运算:bcadd、bcsub、bcmul、bcdiv、bcmod、bccomp

bcscale — 设置所有bc数学函数的默认小数点保留位数


说明
bool bcscale ( int $scale )

设置所有bc数学函数的未设定情况下得小数点保留位数.
参数

scale

    小数点保留位数.

返回值

成功时返回 TRUE , 或者在失败时返回 FALSE 。

<?php

// default scale : 3
bcscale ( 3 );
echo  bcdiv ( '105' ,  '6.55957' );  // 16.007

// this is the same without bcscale()
echo  bcdiv ( '105' ,  '6.55957' ,  3 );  // 16.007

?>


bcadd — 2个任意精度数字的加法计算


说明
string bcadd ( string $left_operand , string $right_operand [, int $scale ] )

左操作数和右操作数求和
参数

left_operand

    左操作数,字符串类型
right_operand

    右操作数,字符串类型
scale

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

返回值

2个操作数求和之后的结果以字符串返回


bcsub — 2个任意精度数字的减法


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

左操作数减去右操作数.
参数

left_operand

    字符串类型的左操作数.
right_operand

    字符串类型的右操作数.
scale

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

返回值

返回减法之后结果为字符串类型.


bcmul — 2个任意精度数字乘法计算


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

左操作数乘以右操作数
参数

left_operand

    字符串类型的左操作数.
right_operand

    字符串类型的右操作数.
scale

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

返回值

返回结果为字符串类型.


bcdiv — 2个任意精度的数字除法计算


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

左操作数除以右操作数
参数

left_operand

    左操作数,字符串类型
right_operand

    右操作数,字符串类型
scale

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

返回值

返回结果为字符串类型的结果,如果右操作数是0结果为null


bcmod — 对一个任意精度数字取模


说明
string bcmod ( string $left_operand , string $modulus )

对左操作数使用系数取模
参数

left_operand

    字符串类型的左操作数
modulus

    字符串类型系数

返回值

返回字符串类型取模后结果,如果系数为0则返回null


bccomp — 比较两个任意精度的数字


说明
int bccomp ( string $left_operand , string $right_operand [, int $scale = int ] )

把right_operand和left_operand作比较, 并且返回一个整数的结果.
参数

left_operand

    左边的运算数, 是一个字符串.
right_operand

    右边的运算数, 是一个字符串.
scale

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

返回值

如果两个数相等返回0, 左边的数left_operand比较右边的数right_operand大返回1, 否则返回-1.