我的PHP代码没有添加正确

I making one counter, I am using PHP and almost everything was going fine but i found one small bug in it.

My first two step on counter is going fine that is $bid and $stax.

My last result $pay should be : 8138 + 814 + 448 = 9400 but it is giving me $9,399

My Output is:

Value $8,138 bid $814 tax $448 You Pay $9,399
Value $8,952 bid $895 tax $492 You Pay $10,339
Value $9,847 bid $985 tax $542 You Pay $11,373

Here is my php

<?php
$i = 0;
$v = 8138;  // value : 8138
do {
    $i++;
    $bid  = $v / 10;      // output : $814
    $ftax = $v + $bid;
    $stax = $ftax / 20;  // output : tax $448
    $pay  = $v + $bid + $stax;   // 8138 + 814 + 448 = 9400
    echo "Value $" . number_format($v) . " bid $" . number_format($bid) . " tax $" . number_format($stax) . " You Pay $" . number_format($pay) . "<br />";
    $v = $v * 1.1;
} while ($i <= 2);
?>

Thanks in advance :)

Change your echo line to not drop decimals:

echo "Value $" . $v . " bid $" . $bid . " tax $" . $stax . " You Pay $" . $pay . "<br />";

Value $8138 bid $813.8 tax $447.59 You Pay $9399.39
Value $8951.8 bid $895.18 tax $492.349 You Pay $10339.329
Value $9846.98 bid $984.698 tax $541.5839 You Pay $11373.2619

So 8138 + 813.8 + 447.59 = 9399.39, which rounds down to 9399.

On the other hand, if you round immediately, 813.8 rounds up to 814 and 447.59 rounds up to 448, but you've just added 0.61 to your calculation before even starting, which obviously results in a higher number (9400).

This is why math and science teachers tell you not to round until the very end, since each time you do it, your answer gets less accurate.

Lack of casting/decimals be ye problem matey. har har har

if you dont want to use decimals, round off all division calculations by using round(). Also, multiplications with decimals. See it in action here

$i = 0;
$v = 8138;  // value : 8138
 do {
$i++;
$bid  = round($v / 10);      // output : $814
$ftax = $v + $bid;
$stax = round($ftax / 20);  // output : tax $448
$pay  = $v + $bid + $stax;   // 8138 + 814 + 448 = 9400
echo "Value $" . number_format($v) . " bid $" . number_format($bid) . " tax $" .      number_format($stax) . " You Pay $" . number_format($pay) . "<br />";
$v = round($v * 1.1);