...

View Full Version : Javascript Vat Calculation



stan801003
01-05-2011, 08:57 AM
Hi,

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.

Thanks
Stan



//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);
d=0;
for (i=l; i>=0; i--) {
ret = nums.charAt(i) + ret;
d++;
if (d==3 && i>0) {
ret="," + ret;
d=0;
}
}
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);
document[form][field2].value=total;
document[form][field3].value=pound(sum);
return false;
}

Philip M
01-05-2011, 09: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum