PHP â€“ How to add two arbitrary precision numbers using bcadd() function?

In PHP, bcadd() math function is used to add two arbitrary precision numbers. The bcadd() function takes two random precision numbers as strings and it returns the addition of the two numbers after scaling the result to an identified precision.

string bcadd ( $num_str1,$num_str2, $scaleVal) Parameters The bcadd() 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.

Example 1 - bcadd() PHP function without using $scaleVal parameter <?php // PHP program to illustrate bcadd() function // two input numbers using arbitrary precision$num_string1 = "5";
$num_string2 = "10.555"; // calculates the addition of // the two numbers without$scaleVal
$result = bcadd($num_string1, $num_string2); echo "Output without scaleVal is: ",$result;
?>

Output

Output without scaleVal is: 15

Explanation - In the above PHP example, only two parameters $num_string1 and$num_string2 are used to calculate the addition of two numbers by using bcadd() function. The $scaleval parameter is not used and it gives the output value 15 and removed the precision number after 15. Example 2 - bcadd() PHP function using$scaleVal parameter

Now, let us use the same input values along with $scaleVal parameter and check the output. <?php // PHP program to illustrate bcadd() function // two input numbers using arbitrary precision$num_string1 = "5";
$num_string2 = "10.555"; //using scale value 2$scaleVal = 2;

// two numbers with $scaleVal parameter$result = bcadd($num_string1,$num_string2, $scaleVal); echo "Output with scaleVal is: ",$result;