INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Applications

Simple VAT Calculator by BigBadDave
Posted: 5 Jan 02 (Edited 9 Jun 02)

Here is my simple VAT Calculator.

Main code :

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


Prety simple to understand, just getting a load of variables carrying out calculations and returning results.

Currency Code :

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


Again prety simple to understand just formatting output into currency format

Complete Code :

<html>
  <head>
    <title>
      VAT Calculator
    </title>
    <script>
      // this function is for getting the calculated sum into currency format
      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;
      }
    </script>
  </head>
  <body>
      <form name="form" method="post" action="#" onsubmit="return calcVat('form','input','total','tax','0.175')">
        <table>
          <tr>
            <td>Amount :</td>
            <td><input type="text" name="input" value=""></td>
          </tr>
          <tr>
            <td>VAT :</td>
            <td><input type="text" name="tax" onfocus="blur()"></td>
          </tr>
          <tr>
            <td>Total :</td>
            <td><input type="text" name="total" onfocus="blur()"></td>
          </tr>
          <tr>
            <td><input type="submit" value="calculate" onclick="return calcVat('form','input','total','tax','0.175')"></td>
          </tr>
        </table>
      </form>
  <body>
</html>


Regards

Big Bad Dave


www.byngdesigns.co.uk

davidbyng@hotmail.com

Back to Javascript FAQ Index
Back to Javascript Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close