Search code examples
phpfloating-point-precisionbcmath

php - bcadd has different point precision


On my windows box when I run

  $SR = "0";
  $SPR = "149";
  $SR = bcadd($SR, $SPR);
  echo "$SR"; 

It outputs 149.0000000000

But when I upload the same code to my Linux host, the output is 149.

Why?


Solution

  • probably the "scale" is different on the two environments.

    Try to set the scale with the bcscale function before doing your operations, For example:

    bcscale(3);
    
    $SR = "0";
    $SPR = "149";
    $SR = bcadd($SR, $SPR);
    echo "$SR"; 
    

    Or simply use the third parameter in bcadd to set the scale:

    $SR = "0";
    $SPR = "149";
    $SR = bcadd($SR, $SPR, 3);
    echo "$SR";