note 92357 added to function.bccomp
| From: | Nitrogen at osu1 dot php dot net | Date: | Tue, 21 Jul 2009 13:23:30 +0000 |
| Subject: | note 92357 added to function.bccomp | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158539@lists.php.net to get a copy of this message | ||
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);
// if the two numbers are of equal length, we check digit-by-digit
else {
// remove ending zeroes from decimals and remove point
$Dec1=isset($Tmp1[2])?rtrim(substr($Tmp1[2],1),'0'):'';
$Dec2=isset($Tmp2[2])?rtrim(substr($Tmp2[2],1),'0'):'';
// if the user defined $Scale, then make sure we use that only
if($Scale!=null) {
$Dec1=substr($Dec1,0,$Scale);
$Dec2=substr($Dec2,0,$Scale);
}
// 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<strlen($Num1);$i++) {
if((int)$Num1{$i}>(int)$Num2{$i}) return(1);
else
if((int)$Num1{$i}<(int)$Num2{$i}) return(-1);
}
// if the two numbers have no difference (they're the same).. return 0
return(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.
----
Server IP: 87.124.35.190
Probable Submitter: 217.42.173.142
----
Manual Page -- http://www.php.net/manual/en/function.bccomp.php
Edit -- https://master.php.net/note/edit/92357
Del: integrated -- https://master.php.net/note/delete/92357/integrated
Del: useless -- https://master.php.net/note/delete/92357/useless
Del: bad code -- https://master.php.net/note/delete/92357/bad+code
Del: spam -- https://master.php.net/note/delete/92357/spam
Del: non-english -- https://master.php.net/note/delete/92357/non-english
Del: in docs -- https://master.php.net/note/delete/92357/in+docs
Del: other reasons-- https://master.php.net/note/delete/92357
Reject -- https://master.php.net/note/reject/92357
Search -- https://master.php.net/manage/user-notes.php