bccomp

(PHP 4, PHP 5, PHP 7)

bccomp Сравнение двух чисел произвольной точности

Описание

int bccomp ( string $left_operand , string $right_operand [, int $scale = int ] )

Сравнивает left_operand с right_operand и возвращает целочисленный результат.

Список параметров

left_operand

Левый операнд в виде строки.

right_operand

Правый операнд в виде строки.

scale

Необязательный аргумент scale задает количество цифр после десятичной точки, которое будет участвовать в сравнении.

Возвращаемые значения

Возвращает 0, если числа равны; 1, если left_operand больше, чем right_operand; -1, если меньше.

Примеры

Пример #1 Пример использования bccomp()

<?php

echo bccomp('1''2') . "\n";   // -1
echo bccomp('1.00001''1'3); // 0
echo bccomp('1.00001''1'5); // 1

?>

Коментарии

Автор:
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);
   }
}
?>
2005-02-11 16:03:16
http://php5.kiev.ua/manual/ru/function.bccomp.html
I slapped together min() and max() functions using bccomp().  While min() and max() only take an arbitrary number of args (i.e. max(1, 5, 1235, 12934, 66)) bccomp only takes 2.

Note that this doesn't take into account $scale.

<?php

function bcmax() {
 
$max null;
  foreach(
func_get_args() as $value) {
    if (
$max == null) {
     
$max $value;
    } else if (
bccomp($max$value) < 0) {
     
$max $value;
    }
  }
  return 
$max;
}

function 
bcmin() {
 
$min null;
  foreach(
func_get_args() as $value) {
    if (
$min == null) {
     
$min $value;
    } else if (
bccomp($min$value) > 0) {
     
$min $value;
    }
  }
  return 
$min;
}
?>
2005-10-04 18:41:41
http://php5.kiev.ua/manual/ru/function.bccomp.html
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;
}
?>
2009-03-11 08:35:09
http://php5.kiev.ua/manual/ru/function.bccomp.html
Автор:
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,$BComp($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.
2009-07-21 09:23:29
http://php5.kiev.ua/manual/ru/function.bccomp.html
Автор:
Beware that negative zero does not compare equal to positive zero.
2010-02-17 00:04:46
http://php5.kiev.ua/manual/ru/function.bccomp.html
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(53'>=')); // true

?>

Still works with arbitrary length numbers.
2012-05-07 04:19:54
http://php5.kiev.ua/manual/ru/function.bccomp.html
In PHP 7, bccomp($a, $b) is the same as $a <=> $b.
2016-06-28 18:26:22
http://php5.kiev.ua/manual/ru/function.bccomp.html
Автор:
bccomp - doesn't compare by default more then 16 characters

$number = '-4.444444444444444444444444444444444444444444445';
$precision = 16;
var_dump(bccomp(bcadd($number, '0', $precision), bcadd($number, '0', $precision+1)));

//outputs 0 on PHP 5.3

use strcomp
2017-09-19 02:57:41
http://php5.kiev.ua/manual/ru/function.bccomp.html
BEWARE! left and right operand is string!! so number in E-notation like 9.012E-6  need to be converted with sprintf('%F') to string
2018-02-20 00:01:27
http://php5.kiev.ua/manual/ru/function.bccomp.html

    Поддержать сайт на родительском проекте КГБ