Loading [MathJax]/jax/output/HTML-CSS/jax.js

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


在 PHP 中,**bcsub()** 数学函数用于从另一个数字中减去一个任意精度数字。**bcsub()** 函数将两个任意精度数字作为字符串,并在将结果缩放到确定的精度后给出这两个数字的差。

语法

string bcsub ($num_str1, $num_str2, $scaleVal)

参数

**bcsub()** 数学函数接受三个不同的参数 **numstr1,num_str2** 和 **$scaleVal。**

  • **$num_str1 −** 它表示左操作数,是字符串类型参数。

  • **$num_str2 −** 它表示右操作数,是字符串类型参数。

  • **$scaleVal −** 它是可选的整数类型参数,用于设置结果输出中小数点后的位数。默认情况下返回零值。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

返回值

**bcadd()** 数学函数返回两个数字 **$num_str1** 和 **num_str2** 的差,作为字符串。

示例 1 - 不使用 $scaleVal 参数的 bcsub() PHP 函数

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

在这种情况下,我们将使用相同的输入值,并使用 3 的 **scaleVal**。因此,输出值将显示小数点后 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

更新于: 2021 年 8 月 21 日

678 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告