bccomp在php中什么意思,PHP bccomp()用法及代码示例
PHP中的bccomp()函數是一個內置函數,用于比較兩個任意精度數字。此函數接受兩個任意精度的數字作為字符串,并在將兩個數字進行比較以達到指定的精度后返回兩個數字的比較結果。
用法:
int bccomp ( $num_str1, $num_str2, $scaleVal)
參數:該函數接受三個參數,如上面的語法所示,下面將進行解釋。
$num_str1:此參數為字符串類型,代表左側操作數或我們要進行比較的兩個數字之一。此參數是必需的。
$num_str2:此參數為字符串類型,表示正確的操作數或我們要進行比較的兩個數字之一。此參數是必需的。
$scaleVal:此參數為int類型,是可選的。此參數告訴小數位后將用于比較的位數。此參數的默認值為零。
返回值:該函數基于兩個數字$num_str1和$num_str2的比較返回一個整數值。如果兩個數字相等,則此函數返回零。如果$num_str1大于$num_str2,則此函數返回1;如果$num_str1小于$num_str2,則此函數返回-1。
例子:
Input: $num_str1 = 3.22, $num_str2 = 3
Output: 0
Explanation: Since the parameter $scaleVal is not
specified so no digits after decimal is used in
comparison. So, the value of first parameter which
is 3.22 will be treated as 3 and hence both
parameters are equal.
Input: $num_str1 = 3.222, $num_str2 = 3, $scaleVal = 2
Output: 1
Input: $num_str1 = 3, $num_str2 = 3.222, $scaleVal = 2
Output: -1
以下示例程序旨在說明PHP中的bccomp()函數:
程序1:
// PHP program to illustrate bccomp() function
// input numbers
$num_str1 = "3.12";
$num_str2 = "3";
// calculates the comparison of the two
// numbers when $scaleVal is not specified
$res = bccomp($num_str1, $num_str2, 2);
// both parameters are equal
echo $res;
?>
輸出:
0
程序2:
// PHP program to illustrate bccomp() function
// input numbers
$num_str1 = "3.12";
$num_str2 = "3";
// scale value
$scaleVal = 2;
// calculates the comparison of the two
// numbers when $scaleVal is specified
$res = bccomp($num_str1, $num_str2, $scaleVal);
// first parameter is greater than second
echo $res;
?>
輸出:
1
程序3:
// PHP program to illustrate bccomp() function
// input numbers
$num_str1 = "3";
$num_str2 = "3.12";
// scale value
$scaleVal = 2;
// calculates the comparison of the two
// numbers when $scaleVal is specified
$res = bccomp($num_str1, $num_str2, $scaleVal);
// first parameter is smaller than second
echo $res;
?>
輸出:
-1
總結
以上是生活随笔為你收集整理的bccomp在php中什么意思,PHP bccomp()用法及代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: vs2019Linux守护,Visual
- 下一篇: php正则重复匹配,php – 用于匹配
