Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: bccomp - Manual
[go: Go Back, main page]

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

search for in the

bcdiv" width="11" height="7"/> <bcadd
Last updated: Fri, 22 Aug 2008

view this page in

bccomp

(PHP 4, PHP 5)

bccomp2 つの任意精度数値を比較する

説明

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

left_operandright_operand を比較し、 結果を整数値で返します。

パラメータ

left_operand

左オペランドを表す文字列。

right_operand

右オペランドを表す文字列。

scale

オプションの scale パラメータで、 小数点以下の桁数を指定します。ここまでを使用して比較を行います。

返り値

ふたつのオペランドが等しければ 0、 left_operandright_operand より大きければ返り値は 1、小さければ -1 を返します。

例1 bccomp() の例

<?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 07: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;
}
?>
12-Feb-2005 07: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" width="11" height="7"/> <bcadd
Last updated: Fri, 22 Aug 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites