La classe BcMath\Number

(PHP 8 >= 8.4.0)

Introduction

Une classe pour un nombre de précision arbitraire. Ces objets supportent les opérateurs arithmétiques et de comparaison.

Note: Cette classe n'est pas affectée par la directive INI bcmath.scale définie dans le php.ini.

Note: Le comportement d'un opérateur surchargé est le même que de spécifier null pour le paramètre scale sur la méthode correspondante.

Synopsis de la classe

namespace BcMath;
final readonly class Number implements Stringable {
/* Propriétés */
public string $value;
public int $scale;
/* Méthodes */
public function __construct(string|int $num)
public function add(BcMath\Number|string|int $num, ?int $scale = null): BcMath\Number
public function ceil(): BcMath\Number
public function compare(BcMath\Number|string|int $num, ?int $scale = null): int
public function div(BcMath\Number|string|int $num, ?int $scale = null): BcMath\Number
public function divmod(BcMath\Number|string|int $num, ?int $scale = null): array
public function floor(): BcMath\Number
public function mod(BcMath\Number|string|int $num, ?int $scale = null): BcMath\Number
public function mul(BcMath\Number|string|int $num, ?int $scale = null): BcMath\Number
public function pow(BcMath\Number|string|int $exponent, ?int $scale = null): BcMath\Number
public function powmod(BcMath\Number|string|int $exponent, BcMath\Number|string|int $modulus, ?int $scale = null): BcMath\Number
public function round(int $precision = 0, RoundingMode $mode = RoundingMode::HalfAwayFromZero): BcMath\Number
public function __serialize(): array
public function sqrt(?int $scale = null): BcMath\Number
public function sub(BcMath\Number|string|int $num, ?int $scale = null): BcMath\Number
public function __toString(): string
public function __unserialize(array $data): void
}

Propriétés

value
Une représentation en chaîne d'un nombre de précision arbitraire.
scale
La valeur de l'échelle actuellement définie sur l'objet. Pour les objets résultant de calculs, cette valeur est automatiquement calculée et définie, sauf si le paramètre scale a été défini dans la méthode de calcul.

Sommaire

add a note

User Contributed Notes 2 notes

up
5
harl at gmail dot com
1 year ago
BcMath\Number is one of those classes that overloads boolean casting.
If $z = new BcMath\Number(0) then $z is considered falsy (and hence, for example, empty($z)==true) even though it is a genuine Number object.
up
0
miken32 at gmail dot com
1 month ago
This class overloads many operators so you can do operations more naturally. But a big caveat is that the strict equality operator *does not work* as demonstrated with this code:

<?php
$sum = new BcMath\Number('23.93') + new BcMath\Number(17) - 6;
echo $sum;            // outputs 34.93

if ($sum < 99 && $sum > 34) {
    echo "foo";       // outputs foo
}

$comp = new BcMath\Number('34.93');
if ($sum === $comp) {
    echo "bar";       // outputs nothing!
}

if ($sum == $comp) {
    // yuck, don't do this
}

if ($sum->compare($comp) === 0) {
    echo "baz";       // outputs baz
}
?>
To Top