user3002600
user3002600

Reputation: 165

BcMath Subtracting big numbers + creating a while loop for the difference

I got a question how to round numbers of BCMath? And somehow this code doesn't work properly - cause when I remove the text it becomes xxx.00000 . I need really help on this one I got no idea how should it look like to make it working properly.

Code

    if (isset($_POST['licz'])) {
            $liczba_a='1111111111111111111';
            $liczba_b='1111111111111111100';
            echo $a = round(bcsub($liczba_a, $liczba_b)).'<br>';
            $diffcap = round($a);
            //secure 1

            $i = 0;
            $count = round($diffcap);
            $array= array();

            while ($i < $count) {
               echo 'array '.$b = bcadd($liczba_a, $i).'<br>';
               array_push($array, $b);
               ++$i;
            }
            var_dump($array);

    } else {
            echo "Wpisz liczby.";
    }
    ?>

My output

11 - this is the diff number
array 1111111111111111111
array 1111111111111111112
array 1111111111111111113
array 1111111111111111114
array 1111111111111111115
array 1111111111111111116
array 1111111111111111117
array 1111111111111111118
array 1111111111111111119
array 1111111111111111120
array 1111111111111111121
array(11) { [0]=> string(23) "1111111111111111111
" [1]=> string(23) "1111111111111111112
" [2]=> string(23) "1111111111111111113
" [3]=> string(23) "1111111111111111114
" [4]=> string(23) "1111111111111111115
" [5]=> string(23) "1111111111111111116
" [6]=> string(23) "1111111111111111117
" [7]=> string(23) "1111111111111111118
" [8]=> string(23) "1111111111111111119
" [9]=> string(23) "1111111111111111120
" [10]=> string(23) "1111111111111111121
" }

Upvotes: 1

Views: 154

Answers (1)

sectus
sectus

Reputation: 15464

Assign operator has lower precedence than concatination.

echo 'array '.$b = bcadd($liczba_a, $i).'<br>';
                                       ^ it would be first operation.
                 ^ it would be second opration.

You should add parentheses

echo 'array '.($b = bcadd($liczba_a, $i)).'<br>';

But better to avoid double purpose operations.

$b = bcadd($liczba_a, $i);
echo 'array '. $b .'<br>';

Upvotes: 1

Related Questions