473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

calculate sums to the 2nd decimal

chunk1978
224 100+
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.  
Jan 26 '07 #1
5 3987
acoder
16,027 Expert Mod 8TB
Use
Expand|Select|Wrap|Line Numbers
  1. var result = num.toFixed(2); 
where 2 is the number of decimal places.
Jan 27 '07 #2
chunk1978
224 100+
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.  
Jan 27 '07 #3
acoder
16,027 Expert Mod 8TB
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]
Jan 27 '07 #4
chunk1978
224 100+
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...
Jan 27 '07 #5
chunk1978
224 100+
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.  
Jan 27 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: mark | last post by:
How to calculate the approximation of a given decimal number, say for two places after decimal. like 10.987 should be approximated to 10.99 10.947 should be approximated to 10.95 can someone...
7
by: neverstill | last post by:
Hello- I need to calculate the aspect ratio of an image. This is stumping me... int w = 190; int h = 260; decimal r = w/h; // this is always 0 Now, I have never tried to do this...
6
by: Programador | last post by:
I'm getting this error when running this program: Cannot calculate rate using the arguments provided Module Module1 Sub Main() Rate(360, -694.44444444444446, 244274.69178082192) End Sub
7
by: Jurek | last post by:
I have 10+ experience in C/C++ - mostly automation and graphics. I have never written any business apps though. Recently I've been asked to write a simple report that would calculate sales...
4
by: Rich_C | last post by:
I'm sure this is very simple, but I have very little experience with javascript -- and what I do know isn't helping me here. I have a simple form where users can enter a quantity (qty) and cost...
4
by: Mayhem05 | last post by:
I'm hoping someone can guide me on solving this vexing problem I have with a database I built to track a projects. I'm using MS Access 2003 and here are the basics: the database is designed to...
11
by: Thomas | last post by:
Hi, I'm pretty new to the programming world. I got stuck in the following problem. Please guide me about it. Well I'm trying to get the value of (128^100)^2 I've used long double type but to no...
6
by: Stuart Shay | last post by:
Hello All: I have a array which contains the totals for each month and from this array I want to get a running total for each month decimal month = new decimal; month = 254; (Jan) month =...
26
by: Prime Mover | last post by:
Hello all, I have got the pseudo-code below that I would like to convert to c language. The algorithm calculates Pi value. I am somewhat familiar with C language, but I am just starting to learn...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.