459,576 Members | 1,462 Online
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 Summation Template File
Jul 23 '05 #1
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 . [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; }
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'mgetting 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. -- 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 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'mgetting 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 © JL/RC: FAQ of news:comp.lang.javascript jscr maths, dates, sources. 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 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 © JL/RC: FAQ of news:comp.lang.javascript jscr maths, dates, sources. TP/BP/Delphi/jscr/&c, FAQ items, links. Jul 23 '05 #12

### This discussion thread is closed

Replies have been disabled for this discussion.