BcMath\Number::compare
(PHP 8 >= 8.4.0)
BcMath\Number::compare — Compares two arbitrary precision numbers
说明
Compare two arbitrary precision numbers. This method behaves similar to the spaceship operator.
参数
num- The value to be compared to.
scale-
Specify the
scaleto use for comparison. Ifnull, all digits are used in the comparison.
返回值
Returns 0 if the two numbers are equal,
1 if $this is greater than num,
-1 otherwise.
错误/异常
This method throws a ValueError in the following cases:
numis string and not a well-formed BCMath numeric stringscaleis outside the valid range
示例
示例 #1 BcMath\Number::compare() example when scale is not specified
<?php
$number = new BcMath\Number('1.234');
var_dump(
$number->compare(new BcMath\Number('1.234')),
$number->compare('1.23400'),
$number->compare('1.23401'),
$number->compare(1),
);
?>以上示例会输出:
int(0) int(0) int(-1) int(1)
示例 #2 BcMath\Number::compare() example of explicitly specifying scale
<?php
$number = new BcMath\Number('1.234');
var_dump(
$number->compare(new BcMath\Number('1.299'), 1),
$number->compare('1.24', 2),
$number->compare('1.22', 2),
$number->compare(1, 0),
);
?>以上示例会输出:
int(0) int(-1) int(1) int(0)
参见
- bccomp() - 比较两个任意精度的数字
+添加备注
用户贡献的备注
此页面尚无用户贡献的备注。
备份地址:http://www.lvesu.com/blog/php/bcmath-number.compare.php