update page now

BcMath\Number クラス

(PHP 8 >= 8.4.0)

はじめに

任意精度数値のクラスです。 このオブジェクトは、オーバーロードされた 算術演算子, 比較演算子 をサポートしています。

注意: このクラスは、php.iniで設定された bcmath.scale INI ディレクティブの影響を受けません。

注意: オーバーロードされた演算子の動作は、対応するメソッドで scale パラメータに null を指定した場合と同じです。

クラス概要

namespace BcMath;
final readonly class Number implements Stringable {
/* プロパティ */
public string $value;
public int $scale;
/* メソッド */
public BcMath\Number::round(int $precision = 0, RoundingMode $mode = RoundingMode::HalfAwayFromZero): BcMath\Number
}

プロパティ

value
任意精度数値の文字表現。
scale
オブジェクトに設定されているスケールの値。 計算メソッドで明示的に scale パラメータが設定されていない場合、 この値は自動的に計算されて設定されます。

目次

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
1
miken32 at gmail dot com
6 days 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