In PHP, bcsub() math function is used to subtract one arbitrary precision number from another number. The bcsub() function takes two arbitrary precision numbers as strings and it gives the subtraction of the two numbers after scaling the result to an identified precision.
Syntax
string bcsub ($num_str1, $num_str2, $scaleVal)
Parameters
The bcsub() math function accepts three different parameters $num_str1, $num_str2 and $scaleVal.
$num_str1 − It represents the left operand and it is the string type parameter.
$num_str2 − It represents the right operand and it is the string type parameter.
$scaleVal − It is the optional integer type parameter that is used to set the number of digits after the decimal place in the resulted output. It returns zero value on default.
Return Values
The bcadd() math function returns the subtraction of two numbers $num_str1 and num_str2, as a string.
Example1 - bcsub() PHP function without using $scaleVal parameter
<?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
Output without scaleVal is: 7
Without $scaleVal parameter, the bcsub() function discards the decimal points in the output.
Example 2 − bcsub() PHP function using the $scaleVal parameter
In this case, we will use the same input values with a scaleVal of 3. So, the output value would show 3 digits after the decimal point.
<?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
Output with scaleVal is: 7.555