What is the usage of bcsub in PHP?
The bcsub function in PHP is used for performing subtraction calculations with arbitrary precision.
The syntax is as follows:
bcsub(string $left_operand, string $right_operand, int $scale = 0): string
Parameter explanation:
- $left_operand: The numerical value to be subtracted, represented as a string.
- $right_operand: The number to be subtracted from the right operand, represented as a string.
- $scale: Optional parameter that specifies the number of decimal places in the result, defaulting to 0.
Return value:
- Return the calculation result as a string.
Usage example:
$result = bcsub('10', '5'); // 结果为字符串"5"
$result = bcsub('3.14', '1.23', 2); // 结果为字符串"1.91"
$result = bcsub('1000', '200', 1); // 结果为字符串"800.0"
Attention:
- Both $left_operand and $right_operand parameters must be valid numeric strings, otherwise FALSE will be returned.
- If the $scale parameter is negative, it will be treated as 0.
- Since the bcsub function performs calculations with arbitrary precision, it can handle very large numerical values, but the calculation speed is relatively slow.