View Full Version : Javascript Vat Calculation

01-05-2011, 09:57 AM

I have a piece of code to calculate the vat on a product form i use, which works fine, the problem is now the vat has increased i am not sure how to increase the code to change the vat from 17.5 % to 20 %.

And would be grateful if somebody could point me in the right direction.


//Calculate VAT
function pound(num) {
var i,l,d;
var nums;
var ret;
nums = String(Math.round(num*100));
while (nums.length <3) nums = "0" + nums;
l = nums.length-3;
ret = "." + nums.charAt(l+1) + nums.charAt(l+2);
for (i=l; i>=0; i--) {
ret = nums.charAt(i) + ret;
if (d==3 && i>0) {
ret="," + ret;
ret = "" + ret;
return ret;
// this is the calculation function
function calcVat(form, field, field2, field3, vat) {
var amount = eval(document[form][field].value);
var sum = amount*vat;
var total = pound(amount+sum);
return false;

Philip M
01-05-2011, 10:59 AM
There is nothing in your code to calculate VAT at 17.5%. Somewhere there must be a constant such as var VAT= .175. Simply change that to .2.

This is very old code. There is no need to use eval. Your code to round to 2 decimal places is cumbersome in the extreme.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.