PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

bcdiv> <bcadd
Last updated: Fri, 03 Jul 2009

view this page in

bccomp

(PHP 4, PHP 5)

bccompCompare deux nombres de grande taille

Description

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

Compare l'opérande left_operand avec l'opérande right_operand et retourne le résultat sous forme d'un entier.

Liste de paramètres

left_operand

L'opérande de gauche, sous la forme d'une chaîne de caractères.

right_operand

L'opérande de droite, sous la forme d'une chaîne de caractères.

scale

Le paramètre optionnel scale est utilisé pour définir le nombre de digits après la décimale qui sera utilisé dans la comparaison.

Valeurs de retour

Retourne 0 si les deux opérandes sont égaux, 1 si l'opérande left_operand est plus grand que l'opérande right_operand , -1 sinon.

Exemples

Exemple #1 Exemple avec bccomp()

<?php

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

?>


bcdiv> <bcadd
Last updated: Fri, 03 Jul 2009
 
add a note add a note User Contributed Notes
bccomp
m dot kaczanowski at alianet dot pl
11-Mar-2009 02:35
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;
}
?>
frank at booksku dot com
04-Oct-2005 10:41
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;
}
?>
11-Feb-2005 10:03
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);
   }
}
?>

bcdiv> <bcadd
Last updated: Fri, 03 Jul 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites