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.The BcMath\Number class
(PHP 8 >= 8.4.0)
Introduction
A class for an arbitrary precision number. These objects support overloaded arithmetic and comparison operators.
Note: This class is not affected by the bcmath.scale INI directive set in php.ini.
Note: The behavior of an overloaded operator is the same as specifying
nullfor thescaleparameter on the corresponding method.
Class synopsis
namespace BcMath;
/* Properties */
/* Methods */
public function powmod(BcMath\Number|string|int
$exponent, BcMath\Number|string|int $modulus, ?int $scale = null): BcMath\Numberpublic function round(int
}
$precision = 0, RoundingMode $mode = RoundingMode::HalfAwayFromZero): BcMath\NumberProperties
- value
- A string representation of an arbitrary precision number.
- scale
-
The scale value currently set on the object.
For objects resulting from calculations, this value is automatically computed and set,
unless the
scaleparameter was set in the calculation method.
Table of Contents
- BcMath\Number::add — Adds an arbitrary precision number
- BcMath\Number::ceil — Rounds up an arbitrary precision number
- BcMath\Number::compare — Compares two arbitrary precision numbers
- BcMath\Number::__construct — Creates a BcMath\Number object
- BcMath\Number::div — Divides by an arbitrary precision number
- BcMath\Number::divmod — Gets the quotient and modulus of an arbitrary precision number
- BcMath\Number::floor — Rounds down an arbitrary precision number
- BcMath\Number::mod — Gets the modulus of an arbitrary precision number
- BcMath\Number::mul — Multiplies an arbitrary precision number
- BcMath\Number::pow — Raises an arbitrary precision number
- BcMath\Number::powmod — Raises an arbitrary precision number, reduced by a specified modulus
- BcMath\Number::round — Rounds an arbitrary precision number
- BcMath\Number::__serialize — Serializes a BcMath\Number object
- BcMath\Number::sqrt — Gets the square root of an arbitrary precision number
- BcMath\Number::sub — Subtracts an arbitrary precision number
- BcMath\Number::__toString — Converts BcMath\Number to string
- BcMath\Number::__unserialize — Deserializes a data parameter into a BcMath\Number object
+add a note
User Contributed Notes 2 notes
harl at gmail dot com ¶
1 year ago
miken32 at gmail dot com ¶
2 months 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
}
?>