Correct VAT calculations in PHP
January 4, 2010
Due to the so called 'Credit Crunch' the British government had decided to change the VAT rate for one year between December 2008 and Jan 2010. Many developers in the UK had made the mistake of hardcoding the VAT rate in their code which caused issues.
After discussions with colleagues it became clear that each developer has different approaches to calculating the VAT on a nett price and deducting VAT from a gross price.
According to http://www.hmrc.gov.uk/VAT/forms-rates/rates/calculating.htm the correct ways to calculate VAT are as follows:
/* * Add VAT to a nett price * Using 17.5% VAT */ $nett = 1.00; $gross = $nett * 1.175; echo number_format($gross, 2); // gives 1.18 due to rounding
/* * Deduct VAT from a gross price * Using 17.5% VAT and decimal fraction */ $gross = 1.00; $nett = $gross/1.175; echo number_format($nett,2); // gives 0.85 due to rounding
/* * Deduct VAT from a gross price * Using 17.5% VAT and the 7/47 rule */ $gross = 1.00; $nett = $gross - (($gross*7)/47); echo number_format($nett,2); // gives 0.85 due to rounding