PHP 8.4.0 RC3 available for testing

bccomp

(PHP 4, PHP 5, PHP 7, PHP 8)

bccompVergleich zweier Zahlen beliebiger Genauigkeit

Beschreibung

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

Vergleicht den num1 mit dem num2 und gibt das Ergebnis als Integer-Wert zurück.

Parameter-Liste

num1

Der linke Operand in Stringform.

num2

Der rechte Operand in Stringform.

scale

Der optionale scale-Parameter wird verwendet, um die Anzahl der Dezimalstellen nach dem Komma anzugeben, die für den Vergleich herangezogen werden sollen.

Rückgabewerte

Gibt 0 zurück, wenn beide Operatoren gleich sind, 1, wenn num1 größer ist als num2, und andernfalls -1.

Changelog

Version Beschreibung
8.0.0 scale ist jetzt nullbar.

Beispiele

Beispiel #1 bccomp()-Beispiel

<?php

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

?>
add a note

User Contributed Notes 3 notes

up
26
Robert Lozyniak
14 years ago
Beware that negative zero does not compare equal to positive zero.
up
15
aaugrin at gmail dot com
6 years ago
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
up
-1
m dot kaczanowski at alianet dot pl
15 years ago
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;
}
?>
To Top