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

simple math - eluding me

Status
Not open for further replies.

Wulfgen

Technical User
Dec 31, 2004
283
0
0
US
Hi all,

I'm putting together a form with a simple math routine in it - however the multiply function gets me - I cant seem to get it right - any ideas on where I'm going wrong?

Code:
<HTML>
<HEAD>
<script language="javascript">

// # START "SIMPLE TEXT BOX NUMBER ADDITION" SCRIPT # -->

// this first two parts seem to work. -->

function calcFnl()
{
shipping = document.frmCalculate.shipping.value
shipping = Number(shipping)

Total_Price_to_be_Paid = document.frmCalculate.Total_Price_to_be_Paid.value;
Total_Price_to_be_Paid = Number(Total_Price_to_be_Paid)

Total = (shipping+Total_Price_to_be_Paid)
document.frmCalculate.trb.value = Total

// here is the muliply part. -->

volume = document.frmCalculate.volume.value
volume = Number(volume)

rslt = document.frmCalculate.rslt.value
rslt = Number(rslt)

Final = (trb*volume)
document.frmCalculate.rslt.value = Final
}

// # END "SIMPLE TEXT BOX NUMBER ADDITION" SCRIPT # -->

</script>
</HEAD>
<BODY>
<form name="frmCalculate">
<input name="shipping" type="text" class="textField" onChange="calcFnl()" size="6" maxlength="5">
+</input> 
<input name="Total_Price_to_be_Paid" type="text" class="textField" onChange="calcFnl()" size="6" maxlength="7" > 
=
</input>
<input type="text" size="7" name="trb" maxlength="7" class="textField" onChange="calcFnl()">
</input></input>
</input>
  times 
 <input name="volume" type="text" class="textField" id="volume" onChange="calcFnl()" size="6" maxlength="7" >
is =
<input name="rslt" type="text" class="textField" id="rslt" onChange="calcFnl()" size="6" maxlength="7" >
</form>
</BODY>
</HTML>
 
You are using "trb" as if it were a vriable, but it is not, so will probably be returning "undefined" or "NaN".

You should probably also use "parseInt" instead of trying to typecast as a number.

Keeping some consistency in your variable names would also be a good plan (some are 4 letters, some are 4 works with underscores - no consistency whatsoever).

Try this `for size. Note: I've removed these two lines:

Code:
rslt = document.frmCalculate.rslt.value
rslt = Number(rslt)

because you were never actually using the value of "rslt".

Code:
function calcFnl() {
	var frm = document.forms['frmCalculate'].elements;
	
	var shipping = parseInt(frm['shipping'].value, 10);
	var price = parseInt(frm['Total_Price_to_be_Paid'].value, 10);
	var total = shipping + price;
	frm['trb'].value = total;

	var volume = parseInt(frm['volume'].value, 10);
	var final = total * volume;

	frm['rslt'].value = final;
}

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi Billy ... I could'nt quite get it to work right, so I came up with this that appears to function fine (below) -- unfortunately another thing cropped up. The rounding of the two fields called trb and rslt .. they are the sub total and total fields of this form. If you enter arbitrary numbers into the other fields you get results like this 58.400 or 175.200 -- thats using 12.95 as the shipping amount, 45.45 as the total price, and the unit# as 3

How can these two fields (*trb and *rslt) be rounded to the next decimal like this: 58.40 that way they can be passed on as complete numbers.....

any ideas?

Code:
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test calc</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<body leftmargin="0" topmargin="0" rightmargin="0" marginwidth="0" marginheight="0" onLoad="P7_fixSafariBB()">
<table width="25%" height="35"  border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#006699">
  <tr>
    <td><table width="100%"  border="0" cellpadding="10" cellspacing="0" bgcolor="#FFFFFF">
        <tr>
          <td>
		  <FORM>
shipping pricerange
  <INPUT NAME="shipping" TYPE="TEXT" size="7" maxlength="7" onChange="this.form.trb.value = (this.form.shipping.value - 0) + (this.form.Total_Price_to_be_Paid.value - 0)">
<br>
<br>
plus<br>
<br>
Total_Price_to_be_Paid
<INPUT NAME="Total_Price_to_be_Paid" TYPE="TEXT" size="7" maxlength="7" onChange="this.form.trb.value = (this.form.shipping.value - 0) + (this.form.Total_Price_to_be_Paid.value - 0)">
<br>
<br>
equals<br>
<br> 

Sub Total/trb 
<INPUT NAME="trb" TYPE="TEXT" size="7" maxlength="7" onChange="this.form.trb.value = (this.form.shipping.value - 0) + (this.form.Total_Price_to_be_Paid.value - 0)" onfocus="roundNumber()"> 
<br>
<br>
times <br>
<br>
volume/# of units
<INPUT NAME="volume" TYPE="TEXT" size="7" maxlength="7" onChange="this.form.rslt.value = (this.form.trb.value - 0) * (this.form.volume.value - 0)">
<br>
<br> 
result/Total
<INPUT NAME="rslt" TYPE="TEXT" size="7" maxlength="7" onChange="this.form.rslt.value = (this.form.trb.value - 0) * (this.form.volume.value - 0)">
</FORM>
</td>
   </tr>
    </table>
	</td>
  </tr>
</table>
</body>
</html>
 
To convert strings to numbers, you would use:

Code:
var theNum = parseInt(theString, 10);

to truncate numbers to a set number of d.p., you would use:

Code:
var newNum = oldNum.toFixed(2);

where 2 is the number of d.p. you want.

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Kinda - the function works but I ran into the hassle of the figures rounding off - they still came up as figures like 49.5 rather than 49.50

and I'm sorry i didnt get back to you on this - I've noticed that you've asked other's on a numver of times the same "does it work' question - sorry!

The above function was for a paypal script and paypal accepts only two other (hidden) fields besides the standard ones - (1) called ono and the other oso if you use these hidden fields you can add other functionality to paypals buy script.

The biggest bummer was to find out that the owner of the paypal store had set up shipping to be preset at $5 (when they initially opened their account at paypal) -- consequently any amount of items you buy at the store 1 - 100 will only charge $5 for shipping - I can't get that across to them though - sigh

But thank you so much for your efforts - I figure I will use this as my little note at the bottom of my posts
I have no control over the client - or their thought processes
 
they still came up as figures like 49.5 rather than 49.50

Odd - toFixed will convert "49.5" to "49.50" - so if you're not seeing that, perhaps the toFixed call is not being tun for you?

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