0

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...

2
Contributors
2
Replies
10
Views
3 Years
Discussion Span
Last Post by everton.retweets
0

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:

http://www.php.net/manual/en/function.money-format.php

Edited by diafol

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.