Home >Backend Development >PHP Problem >How to implement addition, subtraction, multiplication and division in php function
How to implement addition, subtraction, multiplication and division by PHP functions: 1. bcadd function, used for addition calculation of two arbitrary precision numbers, the syntax is "bcadd($num1, $num2, $scale)"; 2. bcsub Function, used for subtraction of two arbitrary precision numbers, the syntax is "bcsub($num1, $num2, $scale)"; 3. bcmul function, used for multiplication of two arbitrary precision numbers; 4. bcdiv function, used for Computes the division of two arbitrary-precision numbers.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
How do PHP functions implement addition, subtraction, multiplication and division?
Example
$a = 1; $b = 3 ;$c = 2.3 $a *$b *$c 的结果可能变成 6.8999999999999995
bcadd — Addition of two arbitrary-precision numbers
//对 $num1 和 $num2 求和。 bcadd($num1, $num2, $scale) //参数 //$num1 //左操作数,字符串类型。 //$num2 //右操作数,字符串类型。 //$scale //设置结果中小数点后的小数位数。如果未设置,则默认为 0。 //返回值 //以字符串返回两个操作数求和之后的结果。
bcsub — Subtraction of two arbitrary-precision numbers
//$num1 减去 $num2。 bcsub($num1, $num2, $scale) //参数 //$num1 //字符串类型的左操作数。 //$num2 //字符串类型的右操作数。 //$scale //此可选参数用于设置结果中小数点后的小数位数。如果未设置,则默认为 0。 //返回值 //以 string 类型返回减法之后的结果。
bcmul — Multiplication calculation of two arbitrary-precision numbers
//≈num1 乘以 $num2。 bcmul($num1, $num2, $scale) //参数 //$num1 //字符串类型的左操作数。 //$num2 //字符串类型的右操作数。 //$scale //此可选参数用于设置结果中小数点后的小数位数。如果未设置,则默认为 0。 //返回值 //返回字符串类型的结果。
bcdiv — Division calculation of two arbitrary-precision numbers
//$num1 除以 $num2。 bcdiv($num1, $num2, $scale) //参数 //$num1 //被除数,字符串类型。 //$num2 //除数,字符串类型。 //$scale //此可选参数用于设置结果中小数点后的小数位数。如果未设置,则默认为 0。 //返回值 //返回字符串类型的结果。如果 $num2 是 0 结果为 null。
Recommended study: "PHP Video Tutorial》
The above is the detailed content of How to implement addition, subtraction, multiplication and division in php function. For more information, please follow other related articles on the PHP Chinese website!