(PHP 8 >= 8.4.0)
BcMath\Number::mul — 任意精度数値の乗算を行う
$this に num
を掛けます。
num
scale
null
の場合、計算結果の BcMath\Number::scale は自動的に設定されます。
乗算結果を新しい BcMath\Number オブジェクトとして返します。
乗算結果オブジェクトの BcMath\Number::scale が自動的に設定される場合、乗算に使用する2つの数値の BcMath\Number::scale の合計値が使用されます。
つまり、2つの値の BcMath\Number::scale がそれぞれ 2
と 5
の場合、
乗算結果オブジェクトの BcMath\Number::scale は 7
になります。
このメソッドは、以下の場合に ValueError をスローします:
num
が、BCMath で有効でない数値形式の文字列である場合scale
が範囲外の値である場合例1 BcMath\Number::mul() で scale
を指定しない例
<?php
$number = new BcMath\Number('1.234');
$ret1 = $number->mul(new BcMath\Number('2.3456'));
$ret2 = $number->mul('-3.4');
$ret3 = $number->mul(7);
var_dump($number, $ret1, $ret2, $ret3);
?>
上の例の出力は以下となります。
object(BcMath\Number)#1 (2) { ["value"]=> string(5) "1.234" ["scale"]=> int(3) } object(BcMath\Number)#3 (2) { ["value"]=> string(9) "2.8944704" ["scale"]=> int(7) } object(BcMath\Number)#2 (2) { ["value"]=> string(7) "-4.1956" ["scale"]=> int(4) } object(BcMath\Number)#4 (2) { ["value"]=> string(5) "8.638" ["scale"]=> int(3) }
例2 BcMath\Number::mul() で scale
を指定する例
<?php
$number = new BcMath\Number('1.234');
$ret1 = $number->mul(new BcMath\Number('2.3456'), 1);
$ret2 = $number->mul('-3.4', 10);
$ret3 = $number->mul(7, 0);
var_dump($number, $ret1, $ret2, $ret3);
?>
上の例の出力は以下となります。
object(BcMath\Number)#1 (2) { ["value"]=> string(5) "1.234" ["scale"]=> int(3) } object(BcMath\Number)#3 (2) { ["value"]=> string(3) "2.8" ["scale"]=> int(1) } object(BcMath\Number)#2 (2) { ["value"]=> string(13) "-4.1956000000" ["scale"]=> int(10) } object(BcMath\Number)#4 (2) { ["value"]=> string(1) "8" ["scale"]=> int(0) }