Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

var X = Y + Z returns X = "YZ" NOT A NUMBER!!

Status
Not open for further replies.

kenya88

Technical User
Oct 23, 2000
6
US
example:
recheight = 7;
recheight = recheight + 2.75;

this returns 72.75 NOT 9.75!!!!!!!!!!!!!!!!!!

recheight was read in from a cookie----see below.

var the_cookie = document.cookie;
var the_cookie = unescape(the_cookie);
var broken_cookie = the_cookie.split(":");
var recheight = broken_cookie[1];

Thanks in advance; Doug Johnson
 
You need to use eval(); :

Code:
var recheight = 7;
recheight = eval (recheight);
recheight += 2.75;

The resultant output would be 9.75 Regards
David Byng
spider.gif

davidbyng@hotmail.com
if (My Answer == "Good Post") print (
star.gif
);
 
sounds like you need to convert from a string to a number. for example, if recheight is assigned a string representation of a number, it can be converted using parseInteger or parseFloat...

Code:
recheight = "7";
recheight = parseFloat(recheight);
recheight = recheight + 2.75;
[code]

recheight will now equal 9.75.

glenn
 
You can also force number conversions with:

recheight = 7;
recheight = (recheight * 1) + 2.75;
 
Netscape suggests converting in the following way:
recheight = (recheight-0) + 2.75
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top