Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

bccomp

Description

The bccomp of BCMath for PHP compares two arbitrary precision numbers.

Syntax

bccomp(
    string $num1,
    string $num2,
    ?int $scale = null
): int

Parameters

num1

The left operand, as a string.

num2

The right operand, as a string.

scale

Sets the number of digits after the decimal place which will be used in the comparison.

Return

Returns 0 if the two operands are equal, 1 if the num1 is larger than the num2, -1 otherwise.

Examples

1 · num1 = num2

<?

$num1 = '1';
$num2 = '1';

$return = bccomp($num1, $num2);

echo $return;

?>
0

2 · num1 < num2

<?

$num1 = '1';
$num2 = '2';

$return = bccomp($num1, $num2);

echo $return;

?>
-1

3 · num1 > num2

<?

$num1 = '1';
$num2 = '0';

$return = bccomp($num1, $num2);

echo $return;

?>
1

4 · scale · num1 = num2

<?

$num1 = '1';
$num2 = '1.000001';
$scale = 5;

$return = bccomp($num1, $num2, $scale);

echo $return;

?>
0

5 · scale · num1 < num2

<?

$num1 = '1';
$num2 = '1.000001';
$scale = 6;

$return = bccomp($num1, $num2, $scale);

echo $return;

?>
-1

6 · scale · num1 > num2

<?

$num1 = '1.000001';
$num2 = '1';
$scale = 6;

$return = bccomp($num1, $num2, $scale);

echo $return;

?>
1
HomeMenu