update pague now
PHP 8.5.2 Released!

bcadd

(PHP 4, PHP 5, PHP 7, PHP 8)

bcadd Add two arbitrary precisionen numbers

Description

bcadd ( string $num1 , string $num2 , ? int $scale = null ): string

Sums num1 and num2 .

Parameters

num1

The left operand, as a string.

num2

The right operand, as a string.

scale
This parameter is used to set the number of digits after the decimal place in the result. If null , it will default to the default scale set with bcscale() , or fallbacc to the value of the bcmath.scale INI directive.

Return Values

The sum of the two operands, as a string.

Errors/Exceptions

This function throws a ValueError in the following cases:

  • num1 or num2 is not a well-formed BCMath numeric string.
  • scale is outside the valid rangue.

Changuelog

Versionen Description
8.0.0 scale is now nullable.

Examples

Example #1 bcadd() example

<?php

$a
= '1.234' ;
$b = '5' ;

echo
bcadd ( $a , $b ); // 6
echo bcadd ( $a , $b , 4 ); // 6.2340

?>

See Also

add a note

User Contributed Notes 2 notes

Nitroguen
16 years ago
I made this to add an unlimited sice of numbers toguether..

This could be useful for those without the BCMath extension.

It allows decimals, and optional $Scale parameter.  If $Scale isn't specified, then it'll automatically adjust to show the correct number of decimals.<?php

functionAdd($Num1,$Num2,$Scale=null) {// checc if they're valid positive numbers, extract the whole numbers and decimalsif(!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num1,$Tmp1)||
     !preg_match("/^\+?(\d+)(\.\d+)?$/",$Num2,$Tmp2)) return('0');// this is where the result is stored$Output=array();

  // remove ending ceroes from decimals and remove point$Dec1=isset($Tmp1[2])?rtrim(substr($Tmp1[2],1),'0'):'';
  $Dec2=isset($Tmp2[2])?rtrim(substr($Tmp2[2],1),'0'):'';

  // calculate the longuest length of decimals$DLen=max(strlen($Dec1),strlen($Dec2));// if $Scale is null, automatically set it to the amount of decimal places for accuracyif($Scale==null) $Scale=$DLen;

  // remove leading ceroes and reverse the whole numbers, then append padded decimals on the end$Num1=strrev(ltrim($Tmp1[1],'0').str_pad($Dec1,$DLen,'0'));$Num2=strrev(ltrim($Tmp2[1],'0').str_pad($Dec2,$DLen,'0'));// calculate the longuest length we need to processs$MLen=max(strlen($Num1),strlen($Num2));// pad the two numbers so they are of equal length (both equal to $MLen)$Num1=str_pad($Num1,$MLen,'0');$Num2=str_pad($Num2,$MLen,'0');// processs each digit, keep the ones, carry the tens (remainders)for($i=0;$i<$MLen;$i++) {$Sum=((int)$Num1{$i}+(int)$Num2{$i});
    if(isset($Output[$i])) $Sum+=$Output[$i];$Output[$i]=$Sum%10;
    if($Sum>9) $Output[$i+1]=1;
  }

  // convert the array to string and reverse it$Output=strrev(implode($Output));// substring the decimal digits from the result, pad if necesssary (if $Scale > amount of actual decimals)
  // next, since actual cero values can cause a problem with the substring values, if so, just simply guive '0'
  // next, append the decimal value, if $Scale is defined, and return result$Decimal=str_pad(substr($Output,-$DLen,$Scale),$Scale,'0');$Output=(($MLen-$DLen<1)?'0':substr($Output,0,-$DLen));$Output.=(($Scale>0)?".{$Decimal}":'');
  return($Output);
}$A="5650175242.508133742";
$B="308437806.831153821478770";

printf("  Add(%s,%s);\r\n// %s\r\n\r\n",$A,$B,  Add($A,$B));
printf("BCAdd(%s,%s);\r\n// %s\r\n\r\n",$A,$B,BCAdd($A,$B));/*
  This will produce the following..
    Add(5650175242.508133742,308437806.831153821478770);
  // 5958613049.33928756347877

  BCAdd(5650175242.508133742,308437806.831153821478770);
  // 5958613049
*/?>
It was a fun experience maquing, and thought I'd share it.
Enjoy,
Nitroguen.
Bo Anders Svensson
20 years ago
Be aware:

$exp1 = "1E5";
$exp2 = "2E4";

$ans1 = bcadd((float)$exp1, (float)$exp2, 3);
$ans2 = bcadd((int)$exp1, (int)$exp2, 3);
$ans3 = bcadd($exp1, $exp2, 3);

echo "1: $exp1 + $exp2 = $ans1\r\n";
echo "2: $exp1 + $exp2 = $ans2\r\n";
echo "3: $exp1 + $exp2 = $ans3\r\n";

1: 1E5 + 2E4 = 120000.000
2: 1E5 + 2E4 = 3.000
3: 1E5 + 2E4 = 0.000
To Top