By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

getting the numeric value of a variable

P: 110
I have a webpage that allows insurance agents to enter a group of trucks for an insurance policy. I want to keep track of the number of power units (tractors or trucks), the number of trailers and the total insured value (the sum of all the units' values). So for power units, if the unit being entered is a tractor or a truck I have a variable, nPowerUnits, that increments like this
Expand|Select|Wrap|Line Numbers
  2. nPowerUnits=nPowerUnits+1;
That variable works fine, as does nTrailers. However if I do the same thing with the total insured value Javascript concatenates the values rather than adding them numerically. Thus,
Expand|Select|Wrap|Line Numbers
  1. nTIV=nTIV+document.getElementById('value'+i).value;
results in "0, 01000,0100020000" beginning with the original value of 0 and adding, first 1000, then 20000. The correct answer should be 21000. I have tried using the number() function, as in
Expand|Select|Wrap|Line Numbers
  1. var nValue=document.getElementById('value'+x).value;
  2.       nTIV=number(nTIV)+nValue;
  3.                 number(nTIV)=number(nTIV)+number(nValue);
and so on but I get an 'object expected' error.

How can I add two variable values and get a number instead of a string?
Apr 20 '09 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 5K+
P: 9,731
You need a Capital "N" for Number.

Try the following:
Expand|Select|Wrap|Line Numbers
  1. var nValue=document.getElementById('value'+x).value;
  2. //nTIV=Number(nTIV)+nValue;
  3. nTIV=Number(nTIV)+Number(nValue);
Apr 20 '09 #2

Expert Mod 15k+
P: 16,027
...or use parseInt().
Apr 21 '09 #3

Post your reply

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