PHP – 如何使用bcadd()函數加入兩個任意精確度的數字?

WBOY
發布: 2023-09-14 18:38:01
轉載
858 人瀏覽過

PHP – 如何使用bcadd()函数添加两个任意精度的数字?

在 PHP 中,bcadd() 數學函數用來將兩個任意精確度的數字相加。 bcadd() 函數將兩個隨機精度數字作為字串,並在將結果縮放到確定的精度後返回這兩個數字的相加。

語法

string bcadd ( $num_str1, $num_str2, $scaleVal)
登入後複製

參數

bcadd() 數學函數接受三個不同的參數:$num_str1、$num_str2 $scaleVal。

  • $num_str1 - 代表左運算元,為字串型別參數。

  • $num_str2 - 代表右運算元,為字串型別參數。

  • $scaleVal - 可選參數,用於設定結果輸出中小數點後的位數。預設回傳 0。

傳回值

bcadd() 數學函數傳回兩個運算元的總和< strong>$num_str1  num_str2,作為字串。

範例1 - 不使用$scaleVal 參數的bcadd() PHP 函數

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

   // calculates the addition of
   // the two numbers without $scaleVal
   $result = bcadd($num_string1, $num_string2);
   echo "Output without scaleVal is: ", $result;
?>
登入後複製

輸出

Output without scaleVal is: 15
登入後複製

說明- 在上面的PHP 範例中,只使用兩個參數$num_string1$num_string2 來計算加法使用bcadd() 函數計算兩個數字。未使用 $scaleval 參數,它給出輸出值 15,並刪除 15 之後的精度數字。

範例 2 - 使用 $ 的 badd() PHP 函數scaleVal 參數

#現在,讓我們使用相同的輸入值和 $scaleVal 參數並檢查輸出。

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

   //using scale value 2
   $scaleVal = 2;

   // calculates the addition of
   // two numbers with $scaleVal parameter
   $result = bcadd($num_string1, $num_string2, $scaleVal);
   echo "Output with scaleVal is: ", $result;
?>
登入後複製

輸出

Output with scaleVal is: 15.55
登入後複製

以上是PHP – 如何使用bcadd()函數加入兩個任意精確度的數字?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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