Changing strings to numbers(easy...maybe)

M

MickG

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>
 
E

Evertjan.

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
 
D

Douglas Crockford

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.
 
M

Michael Winter

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
 
R

Randy Webb

Kae said:
MickG wrote:




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.
 
R

Randy Webb

mike said:
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.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Mon, 28 Feb 2005 11:45:00, seen in
MickG said:
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.
 
M

Michael Winter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top