PHP – 如何使用 bcsub() 函式從另一個任意精度數字中減去一個任意精度數字?
在 PHP 中,**bcsub()** 數學函式用於從另一個數字中減去一個任意精度數字。**bcsub()** 函式將兩個任意精度數字作為字串,並在將結果縮放到確定的精度後給出這兩個數字的差。
語法
string bcsub ($num_str1, $num_str2, $scaleVal)
引數
**bcsub()** 數學函式接受三個不同的引數 **$num_str1, $num_str2** 和 **$scaleVal。**
**$num_str1 −** 它表示左運算元,是字串型別引數。
**$num_str2 −** 它表示右運算元,是字串型別引數。
**$scaleVal −** 它是可選的整數型別引數,用於設定結果輸出中小數點後的位數。預設情況下返回零值。
返回值
**bcadd()** 數學函式返回兩個數字 **$num_str1** 和 **num_str2** 的差,作為字串。
示例 1 - 不使用 $scaleVal 引數的 bcsub() PHP 函式
<?php // PHP program to illustrate bcadd() function // two input numbers using arbitrary precision $num_string1 = "10.555"; $num_string2 = "3"; // calculates the addition of // two numbers without $scaleVal parameter $result = bcsub($num_string1, $num_string2); echo "Output without scaleVal is: ", $result; ?>
輸出
Output without scaleVal is: 7
在不使用 **$scaleVal** 引數的情況下,**bcsub()** 函式會丟棄輸出中的小數點。
示例 2 - 使用 $scaleVal 引數的 bcsub() PHP 函式
在這種情況下,我們將使用相同的輸入值,並使用 3 的 **scaleVal**。因此,輸出值將顯示小數點後 3 位。
<?php // PHP program to illustrate bcsub() function // two input numbers using arbitrary precision $num_string1 = "10.5552"; $num_string2 = "3"; //using scale value 3 $scaleVal = 3; // calculates the addition of // two numbers without $scaleVal parameter $result = bcsub($num_string1, $num_string2, $scaleVal); echo "Output with scaleVal is: ", $result; ?>
輸出
Output with scaleVal is: 7.555
廣告