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


在 PHP 中,**bcdiv()** 數學函式用於將一個任意精度數字除以另一個數字。**bcdiv()** 函式將兩個任意精度數字作為字串,並將結果作為兩個數字的除法,並將結果縮放為已識別的精度。或者,我們可以說 **bcdiv()** PHP 函式將被除數除以除數。

語法

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

引數

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

  • **$num_string1 -** 它表示被除數,它是字串型別引數。

  • **$num_string2 -** 它表示除數,它是字串型別引數。

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

返回值

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

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

<?php
   // PHP program to illustrate bcdiv() function
   // two input numbers using arbitrary precision
   $num_string1 = "22.5552"; // Dividend numbers
   $num_string2 = "5";       // divisor numbers
   $result = bcdiv($num_string1, $num_string2);
   echo "Output without using Scale Value: ", $result;
?>

輸出

Output without using Scale Value: 4

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

現在,我們將使用 4 的比例值獲取相同的輸入值並檢查輸出。

<?php
   // PHP program to illustrate bcdiv() function
   // two input numbers using arbitrary precision
   $num_string1 = "22.5552"; // Dividend numbers
   $num_string2 = "5";       // divisor numbers

   // using scale value 4
   $scaleVal = 4;

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

輸出

Output with Scale Value is: 4.5110

更新於: 2021年8月21日

653 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.