Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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;
          }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,151
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    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.


  •  

    Posting Permissions

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