PHP - 如何使用bcsub()函數從一個任意精確度的數中減去另一個數?

王林
發布: 2023-09-11 06:02:01
轉載
1623 人瀏覽過

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 - 使用bcsub() PHP函數而不使用$scaleVal參數

<?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 - bcsub () 使用 $scaleVal 參數的 PHP 函數

在本例中,我們將使用 scaleVal 為 3 的相同輸入值。因此,輸出值將在之後顯示 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
登入後複製

以上是PHP - 如何使用bcsub()函數從一個任意精確度的數中減去另一個數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!