Connecting Tech Pros Worldwide Forums | Help | Site Map

calculate sums to the 2nd decimal

chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#1: Jan 26 '07
hey... can anyone show me how to calculate sums to the 2nd decimal only? like:

5.50 + 4.00 = 9.50

i seriously have zero idea and could defo use some help...thanks!

Expand|Select|Wrap|Line Numbers
  1.  
  2. <script type="text/javascript">
  3. <!--
  4. function InvoiceSubTotal(form)
  5.     {
  6.     var a = (form.assignmentinvoice.value != '') ? eval(form.assignmentinvoice.value) : 0;
  7.     var b = (form.printinginvoice.value != '') ? eval(form.printinginvoice.value) : 0;
  8.     var c = (form.shippinginvoice.value != '') ? eval(form.shippinginvoice.value) : 0;
  9.     form.invoicesubtotal.value = a + b + c;
  10. }
  11. //-->
  12. </script>
  13.  
  14. </head>
  15.  
  16. <html>
  17. <body>
  18. <label></label>
  19. <p>&nbsp;</p>
  20. <form id="form" name="form" method="post" action="">
  21.   <label>
  22.   <input name="assignmentinvoice" type="text" id="assignmentinvoice" value="3.36" />
  23.   </label>
  24.   <p>
  25.     <label>
  26.     <input name="printinginvoice" type="text" id="printinginvoice" value="8.26" />
  27.     </label>
  28.   </p>
  29.   <p>
  30.     <label>
  31.     <input name="shippinginvoice" type="text" id="shippinginvoice" value="89.22" />
  32.     </label>
  33.   </p>
  34.   <p>
  35.     <label>
  36.     <input type="checkbox" name="checkbox" value="checkbox" onClick="InvoiceSubTotal(this.form);"/>
  37.     </label>
  38.   Click to Calculate! </p>
  39.   <p>Total: 
  40.     <label>
  41.     <input name="invoicesubtotal" type="text" id="invoicesubtotal" />
  42.     </label>
  43.   </p>
  44. </form>
  45. <p>&nbsp;</p>
  46. </body>
  47. </html>
  48.  
  49.  

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jan 27 '07

re: calculate sums to the 2nd decimal


Use
Expand|Select|Wrap|Line Numbers
  1. var result = num.toFixed(2); 
where 2 is the number of decimal places.
chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#3: Jan 27 '07

re: calculate sums to the 2nd decimal


Quote:

Originally Posted by acoder

Use

Expand|Select|Wrap|Line Numbers
  1. var result = num.toFixed(2); 
where 2 is the number of decimal places.

humm... i hope not like this... as this doesn't work... for sure it's way wrong... but am i close?

Expand|Select|Wrap|Line Numbers
  1.  
  2. function InvoiceSubTotal(form)
  3.     {
  4.     var a = (form.assignmentinvoice.value != '') ? eval(form.assignmentinvoice.value) : 0;
  5.     var b = (form.printinginvoice.value != '') ? eval(form.printinginvoice.value) : 0;
  6.     var c = (form.shippinginvoice.value != '') ? eval(form.shippinginvoice.value) : 0;
  7.     var total = (form.invoicesubtotal.value = a + b + c);
  8.     var result = num.toFixed(2);
  9.     form.invoicesubtotal.value = total;
  10.     form.invoicesubtotal.value = result;
  11. }
  12.  
  13.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Jan 27 '07

re: calculate sums to the 2nd decimal


Quote:

Originally Posted by chunk1978

humm... i hope not like this... as this doesn't work... for sure it's way wrong... but am i close?

Expand|Select|Wrap|Line Numbers
  1.  
  2. function InvoiceSubTotal(form)
  3.     {
  4.     var a = (form.assignmentinvoice.value != '') ? eval(form.assignmentinvoice.value) : 0;
  5.     var b = (form.printinginvoice.value != '') ? eval(form.printinginvoice.value) : 0;
  6.     var c = (form.shippinginvoice.value != '') ? eval(form.shippinginvoice.value) : 0;
  7.     var total = (form.invoicesubtotal.value = a + b + c);
  8.     var result = num.toFixed(2);
  9.     form.invoicesubtotal.value = total;
  10.     form.invoicesubtotal.value = result;
  11. }
  12.  
  13.  

One mistake is in the following line:
Expand|Select|Wrap|Line Numbers
  1. var total = (form.invoicesubtotal.value = a + b + c);
Change that to
Expand|Select|Wrap|Line Numbers
  1. var total = a + b + c;
Also, num was only an example. Replace that with total. Why set the invoice subtotal to both total and then result. Just set it to result.

One final thing: don't use eval. Use parseFloat instead:
Expand|Select|Wrap|Line Numbers
  1. var a = (form.assignmentinvoice.value != '') ? parseFloat(form.assignmentinvoice.value) : 0;
parseFloat will return NaN if not a number. In that case check using isNaN:
Expand|Select|Wrap|Line Numbers
  1. if (isNaN(a)) a = 0;
or you could just include it when setting a, b, and c if you like your cryptic coding!

One final, final thing is that in your original post, your html is incorrect. The Javascript goes in the head section but html comes before head like:
[HTML]<html>
<head>
...
</head>
<body>
...
</body>
</html>[/HTML]
chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#5: Jan 27 '07

re: calculate sums to the 2nd decimal


ok... so this is what i have now, but it's still not working :-/

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. <!--
  5. function InvoiceSubTotal(form)
  6.     {
  7.     var a = (form.assignmentinvoice.value != '') ? parseFloat(form.assignmentinvoice.value) : 0; if (isNaN(a)) a = 0;
  8.     var b = (form.printinginvoice.value != '') ? parseFloat(form.printinginvoice.value) : 0; if (isNaN(b)) b = 0;
  9.     var c = (form.shippinginvoice.value != '') ? parseFloat(form.shippinginvoice.value) : 0; if (isNaN(c)) c = 0;
  10.     var total = a + b + c;
  11.     form.invoicesubtotal.value = total;
  12.     total.toFixed(2);
  13. }
  14. //-->
  15. </script>
  16.  
  17. </head>
  18.  
  19.  
  20. <body>
  21. <label></label>
  22. <p>&nbsp;</p>
  23. <form id="form" name="form" method="post" action="">
  24.   <label>
  25.   <input name="assignmentinvoice" type="text" id="assignmentinvoice" value="3.356" />
  26.   </label>
  27.   <p>
  28.     <label>
  29.     <input name="printinginvoice" type="text" id="printinginvoice" value="8.26" />
  30.     </label>
  31.   </p>
  32.   <p>
  33.     <label>
  34.     <input name="shippinginvoice" type="text" id="shippinginvoice" value="89.2554332" />
  35.     </label>
  36.   </p>
  37.   <p>
  38.     <label>
  39.     <input type="checkbox" name="checkbox" value="checkbox" onClick="InvoiceSubTotal(this.form);"/>
  40.     </label>
  41.   Click to Calculate! </p>
  42.   <p>Total: 
  43.     <label>
  44.     <input name="invoicesubtotal" type="text" id="invoicesubtotal" />
  45.     </label>
  46.   </p>
  47. </form>
  48. <p>&nbsp;</p>
  49. </body>
  50. </html>
  51.  
thanks for the help acoder... i always do appreciate it...
chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#6: Jan 27 '07

re: calculate sums to the 2nd decimal


ouf! nevermind... i got it! woo hoo! :-D

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <!--
  3. function InvoiceSubTotal(form)
  4.     {
  5.     var a = (form.assignmentinvoice.value != '') ? parseFloat(form.assignmentinvoice.value) : 0; if (isNaN(a)) a = 0;
  6.     var b = (form.printinginvoice.value != '') ? parseFloat(form.printinginvoice.value) : 0; if (isNaN(b)) b = 0;
  7.     var c = (form.shippinginvoice.value != '') ? parseFloat(form.shippinginvoice.value) : 0; if (isNaN(c)) c = 0;
  8.     var total = a + b + c;
  9.     form.invoicesubtotal.value = total.toFixed(2)
  10.  
  11. }
  12. //-->
  13. </script>
  14.  
Reply