Beware that negative zero does not compare equal to positive zero.
Documentation on bccomp
bccomp = Compare two arbitrary precision numbers
Compares the left_operand to the right_operand and returns the result as an integer.
left_operand The left operand, as a string. right_operand The right operand, as a string. scale The optional scale parameter is used to set the number of digits after the decimal place which will be used in the comparison.
Usage, params, and more on bccomp
int bccomp ( string $left_operand
, string $right_operand
[, int $scale
= 0 ] )
left_operand
The left operand, as a string. right_operand
The right operand, as a string. scale
The optional scale
parameter is used to set the number of digits after the decimal place which will be used in the comparison.
Returns 0 if the two operands are equal, 1 if the left_operand
is larger than the right_operand
, -1 otherwise.
Notes and warnings on bccomp
Basic example of how to use: bccomp
Example #1 bccomp() example
<?php
echo bccomp('1', '2') . "\n"; // -1
echo bccomp('1.00001', '1', 3); // 0
echo bccomp('1.00001', '1', 5); // 1
?>
Improvement of functions bcmax() and bcmin() originaly written by frank at booksku dot com
<?php
function 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;
}
?>
Note that the above function defeats the purpose of BCMath functions, for it uses the 'conventional' < operator.
Instead, it should be:
<?php
function my_bccomp_zero($amount, $scale)
{
if (@$amount{0}=="-")
{
return bccomp($amount, '-0.0', $scale);
}
else
{
return bccomp($amount, '0.0', $scale);
}
}
?>
You can wrap this function with version_compare() to have support for operators and friendlier (boolean) return values.
<?php
function _bccomp($a, $b, $operator = '=')
{
return version_compare(bccomp($a, $b), 0, $operator);
}
var_dump(_bccomp(5, 3, '>=')); // true
?>
Still works with arbitrary length numbers.
I made this to compare an unlimited size of numbers..
This could be useful for those without the BCMath extension.
It allows decimals, and option $Scale parameter. If $Scale isn't specified, then it'll automatically adjust to the correct number of decimals to compare.
<?php
function Comp($Num1,$Num2,$Scale=null) {
// check if they're valid positive numbers, extract the whole numbers and decimals
if(!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num1,$Tmp1)||
!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num2,$Tmp2)) return('0');
// remove leading zeroes from whole numbers
$Num1=ltrim($Tmp1[1],'0');
$Num2=ltrim($Tmp2[1],'0');
// first, we can just check the lengths of the numbers, this can help save processing time
// if $Num1 is longer than $Num2, return 1.. vice versa with the next step.
if(strlen($Num1)>strlen($Num2)) return(1);
else {
if(strlen($Num1)<strlen($Num2)) return(-1);
Other code examples of bccomp being used
Improvement of functions bcmax() and bcmin() originaly written by frank at booksku dot com
<?php
function 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;
}
?>
Note that the above function defeats the purpose of BCMath functions, for it uses the 'conventional' < operator.
Instead, it should be:
<?php
function my_bccomp_zero($amount, $scale)
{
if (@$amount{0}=="-")
{
return bccomp($amount, '-0.0', $scale);
}
else
{
return bccomp($amount, '0.0', $scale);
}
}
?>