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

calculations!! 1

Status
Not open for further replies.

TrueJoker

Technical User
Jun 14, 2006
115
GB
I am trying to perform a calculation on a web form that will take two seperate quantities plus them together and then multiplied by the quantity to end up with the total cost for that item. The snippet of the calculation i am using at the moment is as follows:

Code:
  if (document.form1.qa.value > "")
     { qa = document.form1.qa.value };
  document.form1.qa.value = eval(qa);
  if (document.form1.qb.value > "")
  	 { qb= document.form1.qb.value };
  document.form1.qb.value = eval (qb);
  tq = qa + qb;
  document.form1.tq.value = dm(eval(tq));

  totala = tq * cost;
  document.form1.totala.value = dm(eval(totala));

now this calculation does work in a way, but not the correct way. I have discovered that for some strange reason qa and qb are no added together but put together to form one number, for example if qa = 1 and qb = 2 the result would be 12 instead of 3

Any ideas why this is happening?
 
You need to convert them from strings to numbers. Try this:

Code:
var frm = document.forms['form1'].elements;

var qaVal = parseInt(frm['qa'].value, 10);
var qbVal = parseInt(frm['qb'].value, 10);

if (!isNaN(qaVal) && !isNaN(qbVal)) {
	var tqVal = qaVal + qbVal;
	frm['tq'].value = tqVal;
	var totala = tqVal * cost;
	frm['totala'].value = dm(totala);
}

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thats great, that worked pefectly, But and there is always a but :( it has thrown out my other calculations such as totaling up a number of totals to produc a grand total! i get a "NaN.00" result in the grand total field now!

Code:
subtotal = totala + totalb + totalc + totald + totale + totalf + totalg + totalh + totali + totalj + totalk +
  totall + totalm + totaln + totalo + totalp + totalq + totalr + totals + totalt + totalu + totalv + totalw + 
  totalx + totaly + totalz + totalaa + totalbb + totalcc + totaldd;
  document.form1.subtotal.value = dm(eval(subtotal));

This is the cal im using at the moment to give me a grand total.

please help :(
 
ah disregard last post it just started working. There was a tiny spelling mistake earlier in the code.

Thank you for your help.
 
I've just realised why it wasnt working before.

It is because all of the fields did not have a value!

is it possible to default the fields that the user can enter the quantity into to 0? if so how do i do this? i have tried stating that their starting value is 0 until changed but this does not work. e.g.

Code:
qa = 0; qb = 0;

that is how i am attempting to set their value to 0.
 
You could either deliver the fields to the page with a "0" value in by default, or use this:

Code:
var frm = document.forms['form1'].elements;

var qaVal = parseInt(frm['qa'].value, 10);
var qbVal = parseInt(frm['qb'].value, 10);

if (isNaN(qaVal)) qaVal = 0;
if (isNaN(qbVal)) qbVal = 0;

var tqVal = qaVal + qbVal;
frm['tq'].value = tqVal;
var totala = tqVal * cost;
frm['totala'].value = dm(totala);

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
excellent that all works now thank you very much :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top