Add Float Numbers using JavaScript and store result in cookie

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • buntyindia
    New Member
    • Jun 2007
    • 101

    Add Float Numbers using JavaScript and store result in cookie

    Hi,

    I have a calculator with seven textBox to add Float numbers upto 2 decimal:

    I have created following function in js:

    Code:
    function total_expenses() {
    	// reteriving all the values from textboxes and parsing string to Float
    	var txtBx1 = parseFloat(document.getElementById('textField01').value);
    	var txtBx2 = parseFloat(document.getElementById('textField02').value);
    	var txtBx3 = parseFloat(document.getElementById('textField03').value);
    	var txtBx4 = parseFloat(document.getElementById('textField04').value);
    	var txtBx5 = parseFloat(document.getElementById('textField05').value);
    	var txtBx6 = parseFloat(document.getElementById('textField06').value);
    	var txtBx7 = parseFloat(document.getElementById('textField07').value);
    	// Storing total
    	var totalVal = txtBx1 + txtBx2 + txtBx3 + txtBx4 + txtBx5 + txtBx6 + txtBx7;
    	// converting numeric to string
    	var strttl = totalVal + "";
    	// Reformatting the total value
    	document.getElementById('textField08').value = ReFrtFld(strttl);
     	estimated_tax_savings();
    }
    
    //reformat to 0.00 format
    function ReFrtFld(mystring) {
    	var num;
    	// using regular expression checking for numeric value upto two decimals.
    	if (mystring.match(/^\d+$|^\d+\.\d{1}$/)) {
    		num = parseFloat(mystring).toFixed(2);
    		return num;
    	} else {
    		return mystring;
    	}
    
    }

    When I enter following data in fields
    2.00
    3.00
    6.00
    ------
    11.00 <----as expected


    But when I enter float value I get
    2.22
    3.33
    --------
    5.5500000000000 01 <---- not expected

    5.55 is expected



    Second thing is after unloading of this page all the field values should be stored in Cookied and when again come on this calculator page value gets loaded from cookie.... [Remeber Browser is still not closed].

    But when closed all the cookies should be deleted..


    Please help me on this..

    Regards,
  • buntyindia
    New Member
    • Jun 2007
    • 101

    #2
    Please Answer to This

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Use Math.round for the first thing.[code=javascript]num = Math.round(num * 100) / 100;[/code]
      And for the second thing, use document.cookie.

      Comment

      • buntyindia
        New Member
        • Jun 2007
        • 101

        #4
        How to delete cookie on close browser.
        Please tell me method other then onunload.

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          You can't really differentiate cross-browser between moving back and forth between pages and closing the browser.

          Comment

          Working...