Calculate VAT on a price and round to two decimal places
Posted On at by milanCalculate VAT on a price and round to two decimal places
Description
This function is a simple PHP VAT calculator. It will calculate the 17.5% UK sales VAT (Value Added Tax) on an amount and then round the price to two (2) decimal places. For example, £5.95 would be £6.99125 with VAT added, which would be rounded to £6.99. The amount of vat can be altered to any amount of sales tax, so you can calculate a new price for any string using any percentage. This could come in useful if you needed to calculate the tax on a product in a shopping cart script.
The code
/*
Usage:
You want to calculate 17.5% VAT on a price of £4.67
$price_without_vat = 4.67
echo vat($price_without_vat);
This would return the new amount with 17.5% added, and would be rounded to 2 decimal places
*/
function vat($price_without_vat) {
$vat = 17.5; // define what % vat is
$price_with_vat = $price_without_vat + ($vat*($price_without_vat/100)); // work out the amount of vat
$price_with_vat = round($price, 2); // round to 2 decimal places
return $price_with_vat;
}
?>
Get the code
Download the file to your computer: Click here to get the file