473,322 Members | 1,425 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,322 software developers and data experts.

Add Float Numbers using JavaScript and store result in cookie

101 100+
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
4 8909
buntyindia
101 100+
Please Answer to This
Jul 26 '08 #2
hsriat
1,654 Expert 1GB
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
buntyindia
101 100+
How to delete cookie on close browser.
Please tell me method other then onunload.
Jul 29 '08 #4
acoder
16,027 Expert Mod 8TB
You can't really differentiate cross-browser between moving back and forth between pages and closing the browser.
Jul 29 '08 #5

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

Similar topics

8
by: David Corby | last post by:
Hi everybody, I've got a problem. I'm trying to round a double to a particular number of significant digits, in this case 5, but I can't figure out a way around getting _exactly_ what I want...
5
by: Bryan R. Meyer | last post by:
I am a relatively new C++ programmer and am attempting to write a function that takes a number of type float and adds commas to it in the appropriate places. In order to manipulate the number to...
25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
8
by: vijay | last post by:
Hello, What happens to float variable in loops. For example, float f=8.7; if(f<8.7) printf("less"); else if(f==8.7) printf("equal"); else if(f>8.7)
0
by: Amit Jamgade | last post by:
Hi, I am making use of cookies to store the Session State information through JavaScript as given below in an ASP Page. Suppose I have XYZ.asp page. The code in this page goes as shown below....
17
by: Sri | last post by:
How do you add an n-bit number in C? Regards, Sri
19
by: morc | last post by:
hey, I have float values that look something like this when they are printed: 6.0E-4 7.0E-4 I don't want them to be like this I want them to be normalized with 4 decimal places.
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.