Cast string to integer 
Author Message
 Cast string to integer

I know this must be simple but I can't seem to get it to work...

Using IE 5.5.

I have a series of <INPUT TYPE="text"> boxes that I want to sum up.  When I
do

document.all("txtWhatever").value I get back a string so that:

var total = 0;

for(i=1; i<19; i++) {
    total = total + document.all("txtWhatever").value;

Quote:
}

concatenates two strings instead of adding two numbers.

I tried playing with the Math and Number objects to no avail.



Sun, 29 Aug 2004 04:25:37 GMT  
 Cast string to integer
It's simple really when you think about it.
total = total/1 + document.all("txtWhatever").value/1;


Quote:
> I know this must be simple but I can't seem to get it to work...

> Using IE 5.5.

> I have a series of <INPUT TYPE="text"> boxes that I want to sum up.  When
I
> do

> document.all("txtWhatever").value I get back a string so that:

> var total = 0;

> for(i=1; i<19; i++) {
>     total = total + document.all("txtWhatever").value;
> }

> concatenates two strings instead of adding two numbers.

> I tried playing with the Math and Number objects to no avail.



Sun, 29 Aug 2004 04:20:49 GMT  
 Cast string to integer
Thanks... boy do I feel quite stupid

Quote:
> It's simple really when you think about it.
> total = total/1 + document.all("txtWhatever").value/1;



> > I know this must be simple but I can't seem to get it to work...

> > Using IE 5.5.

> > I have a series of <INPUT TYPE="text"> boxes that I want to sum up.
When
> I
> > do

> > document.all("txtWhatever").value I get back a string so that:

> > var total = 0;

> > for(i=1; i<19; i++) {
> >     total = total + document.all("txtWhatever").value;
> > }

> > concatenates two strings instead of adding two numbers.

> > I tried playing with the Math and Number objects to no avail.



Sun, 29 Aug 2004 04:54:11 GMT  
 Cast string to integer
No problem.
I didn't want to make you feel stupid.
I learned this in the newsgroup. I lot of people have asked that question.


Quote:
> Thanks... boy do I feel quite stupid


> > It's simple really when you think about it.
> > total = total/1 + document.all("txtWhatever").value/1;



> > > I know this must be simple but I can't seem to get it to work...

> > > Using IE 5.5.

> > > I have a series of <INPUT TYPE="text"> boxes that I want to sum up.
> When
> > I
> > > do

> > > document.all("txtWhatever").value I get back a string so that:

> > > var total = 0;

> > > for(i=1; i<19; i++) {
> > >     total = total + document.all("txtWhatever").value;
> > > }

> > > concatenates two strings instead of adding two numbers.

> > > I tried playing with the Math and Number objects to no avail.



Sun, 29 Aug 2004 05:30:15 GMT  
 Cast string to integer


Quote:
> Thanks... boy do I feel quite stupid


> > It's simple really when you think about it.
> > total = total/1 + document.all("txtWhatever").value/1;

Do this only if you are absolutely sure your input boxes contain valid
numbers. Much neater and safer is to use either Number typecast or
parseInt/parseFloat functions:

total += Number(document.all.txtWhatever.value)

Quote:
> > [/]

--

Gosha
www.stereofrog.com



Sun, 29 Aug 2004 06:20:52 GMT  
 Cast string to integer

Quote:
> Thanks... boy do I feel quite stupid

[snip]

You shouldn't. :-)  If you're used to Java or some other language I
doubt that it would just occur to you.

--

remove subdomain from address when replying



Sun, 29 Aug 2004 06:25:13 GMT  
 Cast string to integer
Beware that parseInt() always rounds down?

Math.round() is a better solution if your worried about the mathematical
accuracy. Obviously if you want the result to be accurate (but an integer),
you shouldn't round every number before doing the maths, just the result at
the end.

e.g.

    1.2 + 1.5 + 1.7 = 4.4

when rounded, this becomes 4, whereas (if each number is rounded before the
calculation is done):

    1 + 2 + 2 = 5

Chris Barber.


Quote:


> > Thanks... boy do I feel quite stupid
> [snip]

> You shouldn't. :-)  If you're used to Java or some other language I
> doubt that it would just occur to you.

> --

> remove subdomain from address when replying



Sun, 29 Aug 2004 08:09:51 GMT  
 Cast string to integer

Quote:

> I know this must be simple but I can't seem to get it to work...

> Using IE 5.5.

> I have a series of <INPUT TYPE="text"> boxes that I want to sum up.  When I
> do

> document.all("txtWhatever").value I get back a string so that:

> var total = 0;

> for(i=1; i<19; i++) {
>     total = total + document.all("txtWhatever").value;

   total += Number(document.formName.txtWhatever.value);

Quote:
> }

> concatenates two strings instead of adding two numbers.

> I tried playing with the Math and Number objects to no avail.

--

        Martin Honnen
        http://javascript.faqts.com/
        http://home.t-online.de/home/martin.honnen/jsgoddies.html



Sun, 29 Aug 2004 22:12:56 GMT  
 Cast string to integer


Mar 2002 15:25:37 :-

Quote:
>I have a series of <INPUT TYPE="text"> boxes that I want to sum up.  When I
>do

>document.all("txtWhatever").value I get back a string so that:

>var total = 0;
>for(i=1; i<19; i++) {
>    total = total + document.all("txtWhatever").value;
>}

>concatenates two strings instead of adding two numbers.

Eschew surplusage; just change the + to + + for which the second + must
be a unary + which can only result in a number.

--

  <URL:http://www.jibbering.com/faq/> FAQ for comp.lang.javascript by Jim Ley.
  <URL:http://www.merlyn.demon.co.uk/js-index.htm>   JS maths, dates, sources.
  <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.



Sun, 29 Aug 2004 21:09:36 GMT  
 Cast string to integer
My question is so close to this that it is amazing. I need to total all
cells in a column of a table and I have encountered the same problem.

Unfortunately I do not know how to "Eschew surplusage", so I will post a
fresh question.



Quote:


> Mar 2002 15:25:37 :-

> >I have a series of <INPUT TYPE="text"> boxes that I want to sum up.  When
I
> >do

> >document.all("txtWhatever").value I get back a string so that:

> >var total = 0;
> >for(i=1; i<19; i++) {
> >    total = total + document.all("txtWhatever").value;
> >}

> >concatenates two strings instead of adding two numbers.

> Eschew surplusage; just change the + to + + for which the second + must
> be a unary + which can only result in a number.

> --

MSIE 4 ?
>   <URL:http://www.jibbering.com/faq/> FAQ for comp.lang.javascript by Jim
Ley.
>   <URL:http://www.merlyn.demon.co.uk/js-index.htm>   JS maths, dates,
sources.
>   <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics,

links.


Sun, 05 Sep 2004 10:44:32 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. PS string->integers->string

2. Parsing a string for integer

3. Question: How to show stack integer as string?

4. How to make an integer from a string?

5. Converting string to integer/number

6. Integers - Strings?

7. String to Integer ?

8. Converting from a string to an Integer

9. Querystring fails for string, but not integer????

10. How to conver string to integer (ASP)?

11. convert integer to string

12. String to Integer conversion problem

 

 
Powered by phpBB® Forum Software