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

Changing strings to numbers(easy...maybe)

P: n/a
Hi,

Im trying to add 3 numbers together, instead of getting 1+2+3=6, I'm
getting 123, how could I remedy this.

ManyThanks in advance

Michael

<html>
<head>
<title>Summation Template File</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script language="JavaScript">
function addUp() {
var a,b,c,sum;
a = document.fred.aaa.value; // extract the string of text typed into
the input box
b = document.fred.bbb.value;
c = document.fred.ccc.value;
// the problem is on the next line, all the other lines are OK and
shouldn't be changed
sum =a+b+c ;
window.alert("The sum is "+sum);
}
</script>
</head>

<body bgcolor="#FFFFFF" text="#000000">
<form name="fred">
<input type="text" name="aaa" size="5">
<input type="text" name="bbb" size="5">
<input type="text" name="ccc" size="5">
<input type="button" value="Sum Data" onClick="addUp();">
</form>
</body>
</html>
Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
MickG wrote:
Hi,

Im trying to add 3 numbers together, instead of getting 1+2+3=6, I'm
getting 123, how could I remedy this.


use parseInt(number) to get a number

Kae
Jul 23 '05 #2

P: n/a
MickG wrote on 28 feb 2005 in comp.lang.javascript:
Im trying to add 3 numbers together, instead of getting 1+2+3=6, I'm
getting 123, how could I remedy this.


var a='1' // string !
var b='2' // string !
var c='3' // string !

r = a + b + c

alert(r) // returns string 123

r = +a + +b + +c

alert(r) // returns number 6

r = 1*a + 1*b + 1*c

alert(r) // returns number 6
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #3

P: n/a
> I'm trying to add 3 numbers together, instead of getting 1+2+3=6, I'm
getting 123, how could I remedy this. a = document.fred.aaa.value; // extract the string of text typed into
// the input box
b = document.fred.bbb.value;
c = document.fred.ccc.value;
// the problem is on the next line, all the other lines are OK and
// shouldn't be changed
sum =a+b+c ;


sum = (+a) + (+b) + (+c);

See http://www.crockford.com/javascript/survey.html

If you leave out the parens, it could look like

sum = +a++b++c;

which would be very bad. It is better to be clear.
Jul 23 '05 #4

P: n/a
Kae Verens wrote:

To the OP: In future please read the FAQ before posting. See
<URL:http://jibbering.com/faq/>.

[snip]
use parseInt(number) to get a number


parseInt(number, radix)

is almost always preferred.

There are faster ways to coerce a string to a number, namely unary plus:

+str1 + +str2

With the brief snippet shown by the OP, I'd suggest checking the
format of the user input before using it. Whilst parseInt will behave
somewhat sensibly when parsing a non-numeric string, it isn't very
useful to start adding NaNs.

function isInteger(s) {return /^(0|[1-9]\d*)$/.test(s);}

function sum(f) {
var e = f.elements,
a = e['a'],
b = e['b'],
c = e['c'];

if(!isInteger(a)) {
/* Instruct user that input A is invalid. */
return false;
}
if(!isInteger(b)) {
/* Instruct user that input B is invalid. */
return false;
}
if(!isInteger(c)) {
/* Instruct user that input C is invalid. */
return false;
}
e['total'].value = +a + +b + +c;
}

<form action="">
<input type="text" name="a">
<input type="text" name="b">
<input type="text" name="c">
<input type="text" name="total">
<input type="button" value="Sum" onclick="sum(this.form);">
</form>

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5

P: n/a
Kae Verens wrote:
MickG wrote:

Hi,

Im trying to add 3 numbers together, instead of getting 1+2+3=6, I'm
getting 123, how could I remedy this.

use parseInt(number) to get a number


No. You use the unary + to get a number, depending on what you define as
a number.

parseInt('08') is a very prime example of the flaw of parseInt when not
used with a radix.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #6

P: n/a
Or this !!

sum = (a-0) + (b-0) + (c-0);

Jul 23 '05 #7

P: n/a
MickG wrote:
Hi,

Im trying to add 3 numbers together, instead of getting 1+2+3=6, I'm
getting 123, how could I remedy this.


<URL:http://www.jibbering.com/faq/#FAQ4_21>

--
Rob
Jul 23 '05 #8

P: n/a
mike wrote:
Or this !!

sum = (a-0) + (b-0) + (c-0);


No, you should read this groups FAQ first.

Had you done that, you would have prevented at least two problems with
your post. I will leave it to you as an exercise to determine what those
problems are.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #9

P: n/a
JRS: In article <1a**************************@posting.google.com >,
dated Mon, 28 Feb 2005 11:45:00, seen in news:comp.lang.javascript,
MickG <mi*****@hotmail.com> posted :
Im trying to add 3 numbers together,
No, you are not. You are adding 3 strings (probably; at least one is a
string).
instead of getting 1+2+3=6, I'm
getting 123, how could I remedy this.
By reading the newsgroup FAQ.

Michael : Matches to /^-\d+$/ are integers too.
Douglas : sum = (+a) + (+b) + (+c);
If you leave out the parens, it could look like
sum = +a++b++c;


When I leave them out, it looks like
sum = +a + +b + +c;
All:

"Numeric strings" commonly come from reading input controls.

In such cases it is often good to write

var Wotsit = + nameofWotsitcontrol.value
var Al = + nameofAlcontrol.value
var ...
// expression(s) using Wotsit /et al/

Firstly, the desired values are obtained in the desired form; after
that, they are used in expressions which are not cluttered by control
addressing. The gain on clarity will generally outweigh any minor loss
in performance.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #10

P: n/a
Dr John Stockton wrote:

[snip]
Michael : Matches to /^-\d+$/ are integers too.


Yes. It would be acceptable to prefix the number with a plus, too.

Perhaps I should have named the function, isPositiveDecimalInteger,
but that seems a little excessive. :)

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #11

P: n/a
JRS: In article <11**********************@f14g2000cwb.googlegroups .com>
, dated Mon, 28 Feb 2005 16:00:02, seen in news:comp.lang.javascript,
mike <hi****@charter.net> posted :
Or this !!

sum = (a-0) + (b-0) + (c-0);


Even sum = -(-a-b-c) is better than that (and is fairly short).

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.