bccomp在php中什么意思,PHP bccomp()用法及代码示例

PHP中的bccomp()函数是一个内置函数,用于比较两个任意精度数字。此函数接受两个任意精度的数字作为字符串,并在将两个数字进行比较以达到指定的精度后返回两个数字的比较结果。

用法:

int bccomp ( $num_str1, $num_str2, $scaleVal)

参数:该函数接受三个参数,如上面的语法所示,下面将进行解释。

$num_str1:此参数为字符串类型,代表左侧操作数或我们要进行比较的两个数字之一。此参数是必需的。

$num_str2:此参数为字符串类型,表示正确的操作数或我们要进行比较的两个数字之一。此参数是必需的。

$scaleVal:此参数为int类型,是可选的。此参数告诉小数位后将用于比较的位数。此参数的默认值为零。

返回值:该函数基于两个数字$num_str1和$num_str2的比较返回一个整数值。如果两个数字相等,则此函数返回零。如果$num_str1大于$num_str2,则此函数返回1;如果$num_str1小于$num_str2,则此函数返回-1。

例子:

Input: $num_str1 = 3.22, $num_str2 = 3

Output: 0

Explanation: Since the parameter $scaleVal is not

specified so no digits after decimal is used in

comparison. So, the value of first parameter which

is 3.22 will be treated as 3 and hence both

parameters are equal.

Input: $num_str1 = 3.222, $num_str2 = 3, $scaleVal = 2

Output: 1

Input: $num_str1 = 3, $num_str2 = 3.222, $scaleVal = 2

Output: -1

以下示例程序旨在说明PHP中的bccomp()函数:

程序1:

// PHP program to illustrate bccomp() function

// input numbers

$num_str1 = "3.12";

$num_str2 = "3";

// calculates the comparison of the two

// numbers when $scaleVal is not specified

$res = bccomp($num_str1, $num_str2, 2);

// both parameters are equal

echo $res;

?>

输出:

0

程序2:

// PHP program to illustrate bccomp() function

// input numbers

$num_str1 = "3.12";

$num_str2 = "3";

// scale value

$scaleVal = 2;

// calculates the comparison of the two

// numbers when $scaleVal is specified

$res = bccomp($num_str1, $num_str2, $scaleVal);

// first parameter is greater than second

echo $res;

?>

输出:

1

程序3:

// PHP program to illustrate bccomp() function

// input numbers

$num_str1 = "3";

$num_str2 = "3.12";

// scale value

$scaleVal = 2;

// calculates the comparison of the two

// numbers when $scaleVal is specified

$res = bccomp($num_str1, $num_str2, $scaleVal);

// first parameter is smaller than second

echo $res;

?>

输出:

-1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值