...

View Full Version : concatenate added numbers



esthera
07-23-2010, 10:24 AM
i'm trying to concatenate to a string 2 variables that i'm adding together but i'm getting Nan as the result

what am i doing wrong?


str_amount=parseInt(document.getElementById('amount').value);
strfeeamount=parseInt(document.getElementById('feeamount').value);

urltoajax=urltoajax + '&amount=' +str_amount +strfeeamount

Dormilich
07-23-2010, 11:00 AM
because you parse the strings as Numbers (parseInt()) which returns NaN.

esthera
07-23-2010, 11:05 AM
but stramount and strfee are numbers that I want to add together and then concatenate to the string

Dormilich
07-23-2010, 11:07 AM
from the result you get, I only can assume, they’re not valid numbers.

esthera
07-23-2010, 12:51 PM
they are both numbers

1 is 515 and the other is 90

i added an alert and it shows the numbers correctly - it's only wrong when i try concatenating them to the string

please advise

Dormilich
07-23-2010, 12:55 PM
well, are there any other things you do then? because I don’t have that issue. maybe you could post a link to that page, so that I can verify that problem myself.

Old Pedant
07-23-2010, 07:35 PM
Why do you name your variables str_amount and strfeeamount when, in fact, they are *NOT* strings and CAN NOT be???

When you use parseInt as you did, they *WILL* be integers--or they will be NaN. But they won't be strings.



var Amount = parseInt(document.getElementById('amount').value);
var Feeamount = parseInt(document.getElementById('feeamount').value);

if ( isNaN(Amount) || isNaN(Feeamount) )
{
alert("Invalid: one of the amounts is not a number");
} else {
urltoajax += '&amount=' + ( Amount + Feeamount );
}

Notice the parens around the sum of the numbers: Without them, you'd be doing string concatenation because you would be appending to the string '&amount='.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum