In PHP, the bcsub() function is used to subtract an arbitrary precision number from another function that accepts two arbitrary precision numbers as strings and scales the result to the specified precision. Then return the subtraction of two numbers, the syntax is "bcsub($num_str1,$num_str2,$scaleVal)".
The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.
The bcsub() function in PHP is a built-in function used to subtract an arbitrary precision number from another function. This function accepts two arbitrary-precision numbers as strings and returns the subtraction of the two numbers after scaling the result to the specified precision.
Usage:
string bcsub ( $num_str1, $num_str2, $scaleVal)
Parameters: This function accepts three parameters as shown in the above syntax and explained below:
$num_str1: This parameter is a string Type that represents the left operand or one of the two numbers we want to subtract. This parameter is required.
$num_str2: This parameter is of type string and represents the right operand or one of the two numbers we want to subtract. This parameter is required.
$scaleVal: This parameter is of type int and is optional. This parameter tells the number of digits that will appear after the decimal point in the addition result. The default value is zero.
Return value: This function returns the subtraction of two numbers $num_str1 and $num_str2 in string form.
Example:
Input: $num_str1 = 11.222, $num_str2 = 3 Output: 14 Since the parameter $scaleVal is not specified so no digits after decimal is appeared in the result after subtraction. Input: $num_str1 = 11.222, $num_str2 = 3, $scaleVal = 4 Output: 8.2220
The following example program is intended to illustrate the bcsub() function in PHP:
Program 1:
<?php // PHP program to illustrate bcsub() function // input numbers with arbitrary precision $num_str1 = "11.222"; $num_str2 = "3"; // calculates the subtraction of // the two numbers when $scaleVal is // not specified $res = bcsub($num_str1, $num_str2); echo $res; ?>
Output:
8
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the usage of bcsub in php. For more information, please follow other related articles on the PHP Chinese website!