calculate sums to the 2nd decimal

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chunk1978
    New Member
    • Jan 2007
    • 224

    calculate sums to the 2nd decimal

    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!

    Code:
    <script type="text/javascript">
    <!--
    function InvoiceSubTotal(form)
    	{
    	var a = (form.assignmentinvoice.value != '') ? eval(form.assignmentinvoice.value) : 0;
    	var b = (form.printinginvoice.value != '') ? eval(form.printinginvoice.value) : 0;
    	var c = (form.shippinginvoice.value != '') ? eval(form.shippinginvoice.value) : 0;
    	form.invoicesubtotal.value = a + b + c;
    }
    //-->
    </script>
    
    </head>
    
    <html>
    <body>
    <label></label>
    <p>&nbsp;</p>
    <form id="form" name="form" method="post" action="">
      <label>
      <input name="assignmentinvoice" type="text" id="assignmentinvoice" value="3.36" />
      </label>
      <p>
        <label>
        <input name="printinginvoice" type="text" id="printinginvoice" value="8.26" />
        </label>
      </p>
      <p>
        <label>
        <input name="shippinginvoice" type="text" id="shippinginvoice" value="89.22" />
        </label>
      </p>
      <p>
        <label>
        <input type="checkbox" name="checkbox" value="checkbox" onClick="InvoiceSubTotal(this.form);"/>
        </label>
      Click to Calculate! </p>
      <p>Total: 
        <label>
        <input name="invoicesubtotal" type="text" id="invoicesubtotal" />
        </label>
      </p>
    </form>
    <p>&nbsp;</p>
    </body>
    </html>
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use
    Code:
    var result = num.toFixed(2);
    where 2 is the number of decimal places.

    Comment

    • chunk1978
      New Member
      • Jan 2007
      • 224

      #3
      Originally posted by acoder
      Use
      Code:
      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?

      Code:
      function InvoiceSubTotal(form)
      	{
      	var a = (form.assignmentinvoice.value != '') ? eval(form.assignmentinvoice.value) : 0;
      	var b = (form.printinginvoice.value != '') ? eval(form.printinginvoice.value) : 0;
      	var c = (form.shippinginvoice.value != '') ? eval(form.shippinginvoice.value) : 0;
      	var total = (form.invoicesubtotal.value = a + b + c);
      	var result = num.toFixed(2);
      	form.invoicesubtotal.value = total;
      	form.invoicesubtotal.value = result;
      }

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        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?

        Code:
        function InvoiceSubTotal(form)
        	{
        	var a = (form.assignmentinvoice.value != '') ? eval(form.assignmentinvoice.value) : 0;
        	var b = (form.printinginvoice.value != '') ? eval(form.printinginvoice.value) : 0;
        	var c = (form.shippinginvoice.value != '') ? eval(form.shippinginvoice.value) : 0;
        	var total = (form.invoicesubtotal.value = a + b + c);
        	var result = num.toFixed(2);
        	form.invoicesubtotal.value = total;
        	form.invoicesubtotal.value = result;
        }
        One mistake is in the following line:
        Code:
        var total = (form.invoicesubtotal.value = a + b + c);
        Change that to
        Code:
        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:
        Code:
        var a = (form.assignmentinvoice.value != '') ? parseFloat(form.assignmentinvoice.value) : 0;
        parseFloat will return NaN if not a number. In that case check using isNaN:
        Code:
        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]

        Comment

        • chunk1978
          New Member
          • Jan 2007
          • 224

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

          Code:
          <html>
          <head>
          <script type="text/javascript">
          <!--
          function InvoiceSubTotal(form)
          	{
          	var a = (form.assignmentinvoice.value != '') ? parseFloat(form.assignmentinvoice.value) : 0; if (isNaN(a)) a = 0;
          	var b = (form.printinginvoice.value != '') ? parseFloat(form.printinginvoice.value) : 0; if (isNaN(b)) b = 0;
          	var c = (form.shippinginvoice.value != '') ? parseFloat(form.shippinginvoice.value) : 0; if (isNaN(c)) c = 0;
          	var total = a + b + c;
          	form.invoicesubtotal.value = total;
          	total.toFixed(2);
          }
          //-->
          </script>
          
          </head>
          
          
          <body>
          <label></label>
          <p>&nbsp;</p>
          <form id="form" name="form" method="post" action="">
            <label>
            <input name="assignmentinvoice" type="text" id="assignmentinvoice" value="3.356" />
            </label>
            <p>
              <label>
              <input name="printinginvoice" type="text" id="printinginvoice" value="8.26" />
              </label>
            </p>
            <p>
              <label>
              <input name="shippinginvoice" type="text" id="shippinginvoice" value="89.2554332" />
              </label>
            </p>
            <p>
              <label>
              <input type="checkbox" name="checkbox" value="checkbox" onClick="InvoiceSubTotal(this.form);"/>
              </label>
            Click to Calculate! </p>
            <p>Total: 
              <label>
              <input name="invoicesubtotal" type="text" id="invoicesubtotal" />
              </label>
            </p>
          </form>
          <p>&nbsp;</p>
          </body>
          </html>
          thanks for the help acoder... i always do appreciate it...

          Comment

          • chunk1978
            New Member
            • Jan 2007
            • 224

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

            Code:
            <script type="text/javascript">
            <!--
            function InvoiceSubTotal(form)
            	{
            	var a = (form.assignmentinvoice.value != '') ? parseFloat(form.assignmentinvoice.value) : 0; if (isNaN(a)) a = 0;
            	var b = (form.printinginvoice.value != '') ? parseFloat(form.printinginvoice.value) : 0; if (isNaN(b)) b = 0;
            	var c = (form.shippinginvoice.value != '') ? parseFloat(form.shippinginvoice.value) : 0; if (isNaN(c)) c = 0;
            	var total = a + b + c;
            	form.invoicesubtotal.value = total.toFixed(2)
            	
            }
            //-->
            </script>

            Comment

            Working...