Click to See Complete Forum and Search --> : convert string to number


dp_goose
12-03-2002, 12:01 PM
I have string in the form "setuppage,234"

This gets converted to an array like so

str[0]=setuppage
str[1]=234

and str[1] gets stored in another variable pagenum

so that pagenum=234

I want to add a number to the variable ie.

finalpage=pagenum+5

but instead of getting 239, I get 2345

How can I convert pagenum to a number?

Thanks

Goose

Charles
12-03-2002, 12:40 PM
You can force the conversion of a string to a number using theparseFloat() and parseInt top level functions.

http://developer.netscape.com/docs/manuals/js/client/jsref/toplev.htm#1064132
http://developer.netscape.com/docs/manuals/js/client/jsref/toplev.htm#1064173

dp_goose
12-04-2002, 02:06 AM
Thanks very much. It worked a treat.

Kor
01-22-2008, 03:48 AM
Number() method is also a good choice. It has the advantage of returning 0 in case of an empty value