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.
!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)
}// calculate the longest length of decimals$DLen=max(strlen($Dec1),strlen($Dec2));// append the padded decimals onto the end of the whole numbers$Num1.=str_pad($Dec1,$DLen,'0');$Num2.=str_pad($Dec2,$DLen,'0');// check digit-by-digit, if they have a difference, return 1 or -1 (greater/lower than)for($i=0;$i
if((int)$Num1{$i}>(int)$Num2{$i}) return(1);
else
if((int)$Num1{$i}
}// if the two numbers have no difference (they're the same).. return 0return(0);
}
}
}$A="10.50002";$B="10.50001";printf(" Comp(%s,%s); // %s\r\n",$A,$B,Comp($A,$B));printf("BCComp(%s,%s); // %s\r\n",$A,$B,BCComp($A,$B));/*
Comp(10.50002,10.50001); // 1
BCComp(10.50002,10.50001); // 0 (BCComp has a default decimal scale of 0, unless specified)
*/?>
I tried to make this behave like BCComp..
The only difference being mine will compare the decimals by default.. BCComp won't..
.. unless, of course, you specify the amount of decimals to include in the process.
Enjoy,
Nitrogen.