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!

how do i work out a total vaue in dollers cents by * total quantity by 2

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;>//total quantity here
</TD>
<TD>
<DIV align=&quot;center&quot;>
<INPUT type=&quot;text&quot; name=&quot;total price&quot; size=&quot;5&quot;> //total doller value here, I would like to Display a total price as a parseInt()by multiplying the total quantity by a doller value of $18.95</DIV>
</TD>
<TD>
<INPUT type=&quot;button&quot; value=&quot;Calculate&quot; onclick=&quot;return calculate()&quot;>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
 
you should remove the space from your field named &quot;total price&quot; and name it something like &quot;totalPrice&quot;

Code:
function calculate(){
var f = document.form1;
var first = f.t1.value;
var second = f.t2.value;
var third = f.t3.value;
var fourth = parseFloat(first) + parseFloat(second) + parseFloat(third);
f.t4.value = fourth;
f.totalPrice.value = (fourth * 18.95).toFixed(2);
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Jeff,

I have been using the toFixed() method alot here lately. This is a trick I learned from you in previous posts (I thought it would save me the time of writing a function to handle the decimal places). My concern is that Netscape will not know the toFixed() method and raise an error. I use IE and have been unable to confirm this, however in data1's initial post part of the solution I suggested used it and data1 responded with a problem in Netscape. My response then was to remove the toFixed() method. Although I never heard back if that resolved the issue, I assumed it had because data1 then began to take the script to another level. In another post by data1 I belive it was tviman gave a nice snippet for handling dollars and cents. Could you comment on this please? Is the method cross-browser compliant?

Thanks,
Brian
 
sdi,

yes, toFixed() is a DOM method. i only have NS7 and Moz 1.4 to test it in though...i think it will work with NS6+

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
data1,

Just a word of caution. Try to refrain on submitting multiple posts on the same subjects. This will help those within the forum to best advise you with possible solutions, as well as see what others have suggested already. If you have difficulties puting the pieces together with the suggestions you have recieved then respond back to this thread and we can try to help you pull it all together.

Good luck with your script,
Brian

P.S. Thank you for the stars on your previous posts.
(the $39.99 I spent for the book &quot;Beginning JavaScript&quot; by Paul Wilton was well worth the money. I highly recommend it as well as continued use of the Tek-Tips forums)
 
Jeff,

Thank you! (*). I was actually holding off on publishing part of my web page because of not having that question answered. Actually, you have given me many solutions just by reading various posts and the solutions you have provided.

Thank you again,
Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top