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

search for in the

bcdiv> <bcadd
Last updated: Fri, 27 Jun 2008

view this page in

bccomp

(PHP 4, PHP 5)

bccomp — Porównuje dwie liczby o dużej precyzji

Opis

int bccomp ( string $lewy_operand , string $prawy_operand [, int $precyzja ] )

Porównuje lewy_operand z prawy_operand i zwraca rezultat w postaci liczby całkowitej. Opcjonalnego argumentu precyzja używa się do ustalenia ilości cyfr po przecinku, które będą wzięte pod uwagę przy porównywaniu. Funkcja zwraca wartość 0, jeśli operandy są sobie równe. Jeśli lewy_operand jest większy niż prawy_operand , to funkcja zwraca +1, zaś jeśli lewy_operand jest mniejszy niż prawy_operand , to funkcja zwraca -1.

Przykłady

Example #1 bccomp() przykład

<?php 

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

?>


add a note add a note User Contributed Notes
bccomp
frank at booksku dot com
05-Oct-2005 12: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 11: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, 27 Jun 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites