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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

addition vs concatenation

Status
Not open for further replies.

MandoThrasher

Programmer
Jul 12, 2002
49
0
0
US
I'm trying to add to numeric fields together, but it's concatenating the values instead of performing addition. I'm using the "+" operator.

i.e. 2 + 3 is returning 23

What am I doing wrong?
 
this is probably because the original "numbers" are actually strings. use parseInt() to cast them as numbers:
[tt]
var a = "2";
var b = "3";

alert(parseInt(a, 10)+parseInt(b, 10))
[/tt]

note: use "10" as the second argument to parseInt() to ensure that it performs a base-10 cast.

there are other ways to achieve the same result, such as multiplying your strings by 1:
[tt]
var a = "2";
var b = "3";

alert((a * 1) + (b * 1))
[/tt]


=========================================================
while (!succeed) try();
-jeff
 
or:

var a="10";
var b="35";
var c=Number(a)+Number(b);
alert(c);
 
I cannot believe ther is not a FAQ for this. after the question ahs been answered more days then I've been alive. [lol]

anyone up to the task. we need it!!!!!!

if one is not posted in a hour or so I will ahve time to do it otehrwise please someone add one. unless I'm missing a exsisting one _________________________________________________________
for the best results to your questions: FAQ333-2924
[sub]01001111 01101110 01110000 01101110 01110100[/sub]
onpnt2.gif
[sup] [/sub]
 
using the eval() method works too.
var a = 10;
var b = 5;
var c = eval(a) + eval(b);
 
As usual Gary comes in with a Speed test! :)

My results! parseInt() is the winner being the most often on top in both Gecko Based Browsers (GBB) and in Internet Exploder.

Number() comes right behind at a negligeable time behind.

eval() comes in last taking about 10 times more time then our other concurrents. Though I did think of a way to make eval faster by calling it only once. Despite this it still comes in last. This is normal because eval calls a whole new JS interpreter to be loaded into memory.


<script>
var a = &quot;2&quot;, b = &quot;3&quot;, c, i = 10000;

var start = new Date();
var x = i;
while (x--)
{
c = parseInt(a) + parseInt(b);
}
var end = new Date();
document.writeln(&quot;<br>parseInt() \ttook &quot; + (end.getTime() - start.getTime())/i + &quot; milliseconds per round&quot;)

var start = new Date();
var x = i;
while (x--)
{
c = Number(a) + Number(b);
}
var end = new Date();
document.writeln(&quot;<br>Number() \ttook &quot; + (end.getTime() - start.getTime())/i + &quot; milliseconds per round&quot;)

var start = new Date();
var x = i;
while (x--)
{
c = eval(a) + eval(b);
}
var end = new Date();
document.writeln(&quot;<br>eval()\ttook &quot; + (end.getTime() - start.getTime())/i + &quot; milliseconds per round&quot;)

var start = new Date();
var x = i;
while (x--)
{
c = eval(a + &quot; + &quot; + b);
}
var end = new Date();
document.writeln(&quot;<br>special eval()\ttook &quot; + (end.getTime() - start.getTime())/i + &quot; milliseconds per round&quot;)
</script> Gary Haran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top