Web Analytics Made Easy -
StatCounter help with toFixed function - CodingForum

Announcement

Collapse
No announcement yet.

help with toFixed function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • help with toFixed function

    Hi all,

    I just found out about the toFixed function and I got it working great except for formatting one more field. I got the total field to format with two decimal places, but I also want the "line_total2" value to have two decimals and I can only get "3" as opposed to "3.00"

    Every time I try toFixed on document.forms[0].line_total2.value I get "it is not a function.

    Can someone help?


    Code:
    function qtyVerify(form) 
    	{
    		if (document.forms[0].wc_addItem_SBL1.value > 0)
    			{
    				document.forms[0].line_total2.value = (document.forms[0].wc_addItem_SBL1.value * 3.00);
    				var jacob =  (parseFloat(document.forms[0].line_total2.value) + parseFloat(document.forms[0].line_total1.value));
    				document.forms[0].Total.value = jacob.toFixed(2);
    				
    			}	
    	}

  • #2
    Don't forget to parseFloat the document.forms[0].wc_addItem_SBL1.value.
    We can then call toFixed on the resulting calculation before it is stored. Other wise you're most likely multiplying a string by 3.00...

    Code:
    function qtyVerify(form) 
    	{
    		if (document.forms[0].wc_addItem_SBL1.value > 0)
    			{
    				document.forms[0].line_total2.value = (parseFloat(document.forms[0].wc_addItem_SBL1.value) * 3.00).toFixed(2);
    				var jacob =  (parseFloat(document.forms[0].line_total2.value) + parseFloat(document.forms[0].line_total1.value));
    				document.forms[0].Total.value = jacob.toFixed(2);
    				
    			}	
    	}

    Comment


    • #3
      put jacob in parentheses: (jacob).toFixed(2)

      numbers and dot notation are brittle- is it a decimal point or is
      it a property reference?

      you get the same error with 12.toFixed(2) or 12.55.toFixed(2) and so on.
      (12).toFixed(2)....(12.55).toFixed(2)
      Last edited by mrhoo; Oct 5, 2006, 10:55 PM.

      Comment


      • #4
        worked like a charm. Thanks for the help

        Comment

        Working...
        X