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!

Adding total qauntity * a unit cost to display in the total cost text

Status
Not open for further replies.

data1

Programmer
Aug 19, 2003
25
0
0
NZ
<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<SCRIPT language=&quot;JavaScript&quot;>
function calculate(){
var f = document.form1;
var first = f.t1.value;
var second = f.t2.value;
var third = f.t3.value;
f.t4.value = parseFloat(first) + parseFloat(second) + parseFloat(third);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name=&quot;form1&quot;>
<TABLE align=&quot;center&quot; cellspacing=&quot;2&quot; cellpadding=&quot;2&quot; border=&quot;0&quot;>
<TR>
</TABLE>
<TR>
<TD>Inputbox 1
<INPUT type=&quot;text&quot; name=&quot;t1&quot; value=&quot;0&quot;>
</TD>
<TD>Input box2
<INPUT type=&quot;text&quot; name=&quot;t2&quot; value=&quot;0&quot;>
</TD>
<TD>Input box3
<INPUT type=&quot;text&quot; name=&quot;t3&quot; value=&quot;0&quot;>
</TD>

<TD>Result=
<INPUT type=&quot;text&quot; name=&quot;t4&quot; onfocus=&quot;document.form1.t4.blur()&quot; readOnly=&quot;true&quot;>
</TD>
<TD>
<DIV align=&quot;center&quot;>
<INPUT type=&quot;text&quot; name=&quot;total price&quot; size=&quot;5&quot;> // Here in this cell i would like to place a total * Quantity in intergers
</DIV>
</TD>
<TD>
<INPUT type=&quot;button&quot; value=&quot;Calculate&quot; onclick=&quot;return calculate()&quot;>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
 
Which fields on the form denote the quantity and which denote the cost? You are wanting the &quot;total price&quot; to contain the calculated value of the (price per unit * quantity of that unit), is that correct?
 
To display a dollars & cents value from mulitplying qty x amount, I use the following:

*/ adds zeroes if exact change
function cent(amount) {
return (amount == Math.floor(amount)) ? amount + '.00' : ((amount*10 == Math.floor(amount*10)) ? amount + '0' : amount); }

function amtTotal() {
document.form.subttl.value = cent(Math.round(((document.form.qty.value - 0) * (document.form.sellprice.value - 0))*Math.pow(10,2))/Math.pow(10,2));}

There's always a better way. The fun is trying to find it!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top