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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculations

Status
Not open for further replies.

TrueJoker

Technical User
Jun 14, 2006
115
GB
I have just setup an HTML form that uses javascript to total up prices etc and add on vat.

I have come across a slight snag though! the total is not restricted to 2 decimal places.

How do i do this?

the code that performs the calculations is as follows:

Code:
<script language=javascript>
<!--//

function dm(amount) 
{
  string = "" + amount;
  dec = string.length - string.indexOf('.');
  if (string.indexOf('.') == -1)
  return string + '.00';
  if (dec == 1)
  return string + '00';
  if (dec == 2)
  return string + '0';
  if (dec > 3)
  return string.substring(0,string.length-dec+3);
  return string;
}



function calculate()
{

	qtya = 0;  qtyb = 0;  qtyc = 0;  qtyd = 0;  qtye = 0;  qtyf = 0;
	
	c2n1500 = 76.17; c2n1250 = 72.25; c2n1000 = 65.57; c2n0750 = 60.50; c2n0500 = 57.28; c2n0250 = 54.05;
	
	vata = 0.175;
	
	packing = 15;
	
	if (document.form1.qtya.value > "")
		{ qtya = document.form1.qtya.value };
	document.form1.qtya.value = eval(qtya);
	totala = qtya * c2n1500;
	document.form1.totala.value = dm(eval(totala));
	
	if (document.form1.qtyb.value > "")
		{ qtyb = document.form1.qtyb.value };
	document.form1.qtyb.value = eval(qtyb);
	totalb = qtyb * c2n1250;
	document.form1.totalb.value = dm(eval(totalb));
	
	if (document.form1.qtyc.value > "")
		{ qtyc = document.form1.qtyc.value };
	document.form1.qtyc.value = eval(qtyc);
	totalc = qtyc * c2n1000;
	document.form1.totalc.value = dm(eval(totalc));
	
	if (document.form1.qtyd.value > "")
		{ qtyd = document.form1.qtyd.value };
	document.form1.qtyd.value = eval(qtyd);
	totald = qtyd * c2n0750;
	document.form1.totald.value = dm(eval(totald));
	
	if (document.form1.qtye.value > "")
		{ qtye = document.form1.qtye.value };
	document.form1.qtye.value = eval(qtye);
	totale = qtye * c2n0500;
	document.form1.totale.value = dm(eval(totale));
	
	if (document.form1.qtyf.value > "")
		{ qtyf = document.form1.qtyf.value };
	document.form1.qtyf.value = eval(qtyf);
	totalf = qtyf * c2n0250;
	document.form1.totalf.value = dm(eval(totalf));
	
	subtotal = totala + totalb + totalc + totald + totale + totalf;
	document.form1.subtotal.value = dm(eval(subtotal));
	
	vat = vata * subtotal;
	document.form1.vat.value = dm(eval(vat));
	
	incvat = subtotal + vat;
	document.form1.incvat.value = dm(eval(incvat));
	
	if (document.form1.incvat.value < 500)
		{ incvat = document.form1.incvat.value };
	document.form1.incvat.value = eval(incvat);
	grandtotal = incvat + packing;
	document.form1.grandtotal.value = dm(eval(grandtotal));
	
	
} 

//-->
</script>
 
You should not rely on client-side technology to do this, otherwise any script kiddy with a DOM editor will be able to alter prices, etc.

You should do this sort of logic server-side.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top