PHP – 如何使用 bcmul() 函式乘以兩個任意精度的數字?


在PHP中,**bcmul()**數學函式用於將一個任意精度的數字與另一個數字相乘。**bcmul()**函式將兩個任意精度的數字作為字串輸入,並將結果作為兩個數字的乘積輸出,並將結果縮放到指定的精度。

語法

string bcmul( $num_string1, $num_string2, $scaleVal)

引數

**bcmul()**數學函式接受三個不同的引數:**$num_string1, $num_string2**和**$scaleVal。**

  • **$num_string1 -** 表示左運算元,它是字串型別的引數。

  • **$num_string2 -** 表示右運算元,它是字串型別的引數。

  • **$scaleVal -** 這是一個可選的整型引數,用於設定結果輸出中小數點後的位數。它返回預設值0。

返回值

**bcmul()**數學函式返回兩個數字**$num_str1**和**num_str2**的乘積,結果為字串。

示例1 - 不使用$scaleVal引數的bcmul() PHP函式

<?php
   // PHP program to illustrate bcmul() function
   // two input numbers using arbitrary precision
   $num_string1 = "10.5552";
   $num_string2 = "3";

   // calculates the addition of
   // two numbers without $scaleVal parameter
   $result = bcmul($num_string1, $num_string2);

   echo "Output without scaleVal is: ", $result;
?>

輸出

Output without scaleVal is: 31

不使用**scaleVal**引數時,小數點後的數字將被丟棄。

示例2 - 使用$scaleVal()引數的bcmul() PHP函式

在這裡,我們將使用相同的輸入值和4的scale值來檢查輸出。

<?php
   // PHP program to illustrate bcmul() function
   // two input numbers using arbitrary precision
   $num_string1 = "10.5552";
   $num_string2 = "3";

   // using scale value 4
   $scaleVal = 4;

   // calculates the addition of
   // two numbers without $scaleVal parameter
   $result = bcmul($num_string1, $num_string2, $scaleVal);
   echo "Output with scaleVal is:", $result;
?>

輸出

Output with scaleVal is: 31.6656

更新於:2021年8月21日

1K+ 瀏覽量

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.