PHP - 如何使用bcsub()函数从一个任意精度的数中减去另一个数?

来自:互联网
时间:2023-09-11
阅读:

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()函数从一个任意精度的数中减去另一个数?的详细内容。

返回顶部
顶部