By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,213 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

Add Float Numbers using JavaScript and store result in cookie

100+
P: 101
Hi,

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

I have created following function in js:

Expand|Select|Wrap|Line Numbers
  1. function total_expenses() {
  2.     // reteriving all the values from textboxes and parsing string to Float
  3.     var txtBx1 = parseFloat(document.getElementById('textField01').value);
  4.     var txtBx2 = parseFloat(document.getElementById('textField02').value);
  5.     var txtBx3 = parseFloat(document.getElementById('textField03').value);
  6.     var txtBx4 = parseFloat(document.getElementById('textField04').value);
  7.     var txtBx5 = parseFloat(document.getElementById('textField05').value);
  8.     var txtBx6 = parseFloat(document.getElementById('textField06').value);
  9.     var txtBx7 = parseFloat(document.getElementById('textField07').value);
  10.     // Storing total
  11.     var totalVal = txtBx1 + txtBx2 + txtBx3 + txtBx4 + txtBx5 + txtBx6 + txtBx7;
  12.     // converting numeric to string
  13.     var strttl = totalVal + "";
  14.     // Reformatting the total value
  15.     document.getElementById('textField08').value = ReFrtFld(strttl);
  16.      estimated_tax_savings();
  17. }
  18.  
  19. //reformat to 0.00 format
  20. function ReFrtFld(mystring) {
  21.     var num;
  22.     // using regular expression checking for numeric value upto two decimals.
  23.     if (mystring.match(/^\d+$|^\d+\.\d{1}$/)) {
  24.         num = parseFloat(mystring).toFixed(2);
  25.         return num;
  26.     } else {
  27.         return mystring;
  28.     }
  29.  
  30. }
  31.  
  32.  

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.550000000000001 <---- 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,
Jul 25 '08 #1
Share this Question
Share on Google+
4 Replies


100+
P: 101
Please Answer to This
Jul 26 '08 #2

hsriat
Expert 100+
P: 1,654
Use Math.round for the first thing.
Expand|Select|Wrap|Line Numbers
  1. num = Math.round(num * 100) / 100;
And for the second thing, use document.cookie.
Jul 27 '08 #3

100+
P: 101
How to delete cookie on close browser.
Please tell me method other then onunload.
Jul 29 '08 #4

acoder
Expert Mod 15k+
P: 16,027
You can't really differentiate cross-browser between moving back and forth between pages and closing the browser.
Jul 29 '08 #5

Post your reply

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