How to implement addition, subtraction, multiplication and division in php function

藏色散人
Release: 2023-03-17 06:52:02
Original
5014 people have browsed it

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.

How to implement addition, subtraction, multiplication and division in php function

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
Copy after login

bcadd — Addition of two arbitrary-precision numbers

//对 $num1 和 $num2 求和。
bcadd($num1, $num2, $scale)
//参数
//$num1
//左操作数,字符串类型。
//$num2
//右操作数,字符串类型。
//$scale
//设置结果中小数点后的小数位数。如果未设置,则默认为 0。
//返回值
//以字符串返回两个操作数求和之后的结果。
Copy after login

bcsub — Subtraction of two arbitrary-precision numbers

//$num1 减去 $num2。
bcsub($num1, $num2, $scale)
//参数
//$num1
//字符串类型的左操作数。
//$num2
//字符串类型的右操作数。
//$scale
//此可选参数用于设置结果中小数点后的小数位数。如果未设置,则默认为 0。
//返回值
//以 string 类型返回减法之后的结果。
Copy after login

bcmul — Multiplication calculation of two arbitrary-precision numbers

//≈num1 乘以 $num2。
bcmul($num1, $num2, $scale)
//参数
//$num1
//字符串类型的左操作数。
//$num2
//字符串类型的右操作数。
//$scale
//此可选参数用于设置结果中小数点后的小数位数。如果未设置,则默认为 0。
//返回值
//返回字符串类型的结果。
Copy after login

bcdiv — Division calculation of two arbitrary-precision numbers

//$num1 除以 $num2。
bcdiv($num1, $num2, $scale)
//参数
//$num1
//被除数,字符串类型。
//$num2
//除数,字符串类型。
//$scale
//此可选参数用于设置结果中小数点后的小数位数。如果未设置,则默认为 0。
//返回值
//返回字符串类型的结果。如果 $num2 是 0 结果为 null。
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template