
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
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.
Syntax
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.
$scaleVal - It is the optional parameter that is used to set the number of digits after the decimal place in the resulted output. It returns 0 on default.
Return Values
The bcadd() math function returns the sum of two operands $num_str1 and num_str2, as a string.
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; // calculates the addition of // two numbers with $scaleVal parameter $result = bcadd($num_string1, $num_string2, $scaleVal); echo "Output with scaleVal is: ", $result; ?>
Output
Output with scaleVal is: 15.55
- Related Articles
- PHP – How to multiply two arbitrary precision numbers using bcmul() function?
- PHP – How to divide two arbitrary precision numbers using bcdiv() function?
- PHP – How to compare two arbitrary precision numbers using bccomp() function?
- PHP – How to raise an arbitrary precision number to another using bcpow() function?
- PHP – How to subtract one arbitrary precision number from another using bcsub() function?
- PHP – How to get the modulus of an arbitrary precision number using bcmod() function?
- PHP – How to get the square root of an arbitrary precision number using bcsqrt() function?
- How to Add Two Numbers in Golang?
- Add two numbers using ++ operator in C++.
- How to add two complex numbers by passing structure to a function in C language?
- Write a program to add two complex numbers using C
- How to Add two Numbers in Swift Program?
- How to add two Complex numbers in Golang?
- Golang Program to Add Two Complex Numbers by Passing Class to a Function
- Add two unsigned numbers using bits in C++.
