Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Javascript Vat Calculation

1. ## Javascript Vat Calculation

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

Code:
```//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;
}```

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

•

1. ###### JavaScript - Calculation Code With Javascript - AllWebDevHelp.com
06-03-2014, 10:57 AM
2. ###### JavaScript - Form Calculation Using Javascript Not Working - AllWebDevHelp.com
06-02-2014, 10:13 PM
3. ###### JavaScript - Javascript Vat Calculation - AllWebDevHelp.com
04-27-2014, 04:05 PM

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•