Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple VAT Calculator

Applications

Simple VAT Calculator

by  BigBadDave  Posted    (Edited  )
Here is my simple VAT Calculator.

Main code :

[color green]
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;
}
[/color]

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

Currency Code :

[color green]
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;
}
[/color]

Again prety simple to understand just formatting output into currency format

Complete Code :

[color green]
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>
[/color]

Regards

Big Bad Dave

[img http://www.byngdesigns.co.uk/tek-tips/spider.gif]
www.byngdesigns.co.uk

davidbyng@hotmail.com
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top