Hi Everyone, I have been trying to work out how to add 20% vat to a total in php, but for some reason I am unable to.

I have been using the following code, but I keep getting the same price for price_with_vat as I do for price_without_vat

    // price without vat
    $price_without_vat = round($kentPrice, 2);

    // vat          
    $vat = 20.0; // define what % vat is

    // price with vat   
    $price_with_vat = $price_without_vat + ($vat*($price_without_vat/100)); // work out the amount of vat

    // round to 2 decimal places
    $price_with_vat = round($kentPrice, 2); 

Any help with be apprciated...

Member Avatar

Any reason why you're rounding before applying VAT?

$vatRate = 0.2;

$total = $vatRate * $price + $price; 

echo number_format($total, 2, '.', ',');

It's usually preferrable not to round at every calculation as you may encounter rounding errors.

echoing via number_format will not change the original value ($total) in this case.

However you may find the money_format() useful: