Beware that negative zero does not compare equal to positive zero.(PHP 4, PHP 5, PHP 7, PHP 8)
bccomp — 比较两个任意精度的数字
num1左操作数,字符串类型。
num2右操作数,字符串类型。
scalenull,则默认为使用 bcscale() 设置的默认精度,
或者回退到 bcmath.scale INI 指令的值。
如果所有的操作数相等返回 0;如果 num1 大于
num2 时返回 1,否则返回 -1。
| 版本 | 说明 |
|---|---|
| 8.0.0 |
现在 scale 可以为 null。
|
示例 #1 bccomp() 示例
<?php
echo bccomp('1', '2') . "\n"; // -1
echo bccomp('1.00001', '1', 3); // 0
echo bccomp('1.00001', '1', 5); // 1
?>BEWARE! left and right operand is string!! so number in E-notation like 9.012E-6 need to be converted with sprintf('%F') to stringImprovement of functions bcmax() and bcmin() originaly written by frank at booksku dot com<?phpfunction bcmax() { $args = func_get_args(); if (count($args)==0) return false; $max = $args[0]; foreach($args as $value) { if (bccomp($value, $max)==1) { $max = $value; } } return $max;}function bcmin() { $args = func_get_args(); if (count($args)==0) return false; $min = $args[0]; foreach($args as $value) { if (bccomp($min, $value)==1) { $min = $value; } } return $min;}?>If you want to compare 2 decimals for equality, bccomp does not works as expected:bccomp("1.000000000000000","0.999999999999999",2)returns 1Instead, usebcsub("1.000000000000000","0.999999999999999",2) == 0