Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 13th, 2008, 12:05 AM
FAQ server
Guest
 
Posts: n/a
Default FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number? (2008-10-13)

-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a
string to a number?
-----------------------------------------------------------------------

Variables are not typed; their values are. The conversion between a
string and a number happens automatically. Since plus (` + `) is
also used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `.
The string determines what ` + ` does. To overcome this, first convert the
string to a number. For example: ` +varname ` or ` Number(varname) ` or
` parseInt(varname, 10) ` or ` parseFloat(varname) `.
Form control values are strings, as is the result from a ` prompt `
dialog. Convert these to numbers before performing addition by using
the unary ` + ` operator: ` +'1' + 1 ` result is ` 2 `.

Additional Notes:

http://www.jibbering.com/faq/faq_not...e_convert.html

http://msdn2.microsoft.com/en-us/library/67defydd.aspx


--
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers. The sendings of these
daily posts are proficiently hosted by http://www.pair.com.

  #2  
Old October 13th, 2008, 02:45 AM
Fdream
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does 1+1 equal 11? or How do I convert a stringto a number? (2008-10-13)

On Oct 13, 7:00*am, "FAQ server" <javascr...@dotinternet.bewrote:
Quote:
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a
string to a number?
-----------------------------------------------------------------------
>
Variables are not typed; their values are. The conversion between a
string and a number happens automatically. Since plus (` + `) is
also used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `.
The string determines what ` + ` does. To overcome this, first convert the
string to a number. For example: ` +varname ` or ` Number(varname) ` or
` parseInt(varname, 10) ` or ` parseFloat(varname) `.
Form control values are strings, as is the result from a ` prompt `
dialog. Convert these to numbers before performing addition by using
the unary ` + ` operator: ` +'1' + 1 ` result is ` 2 `.
>
Additional Notes:
>
http://www.jibbering.com/faq/faq_not...e_convert.html
>
http://msdn2.microsoft.com/en-us/library/67defydd.aspx
>
--
*Postings such as this are automatically sent once a day. *Their
*goal is to answer repeated questions, and to offer the content to
*the community for continuous evaluation/improvement. *The complete
*comp.lang.javascript FAQ is athttp://jibbering.com/faq/index.html.
*The FAQ workers are a group of volunteers. *The sendings of these
*daily posts are proficiently hosted byhttp://www.pair.com.
just use parseInt:

alert(1 + parseInt('1'));

  #3  
Old October 13th, 2008, 11:15 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: FAQ Topic - Why does 1+1 equal 11? or How do I convert a stringto a number? (2008-10-13)

Fdream wrote:
Quote:
On Oct 13, 7:00 am, "FAQ server" <javascr...@dotinternet.bewrote:
Quote:
>-----------------------------------------------------------------------
>FAQ Topic - Why does 1+1 equal 11? or How do I convert a
>string to a number?
>-----------------------------------------------------------------------
>>
>Variables are not typed; their values are. The conversion between a
>string and a number happens automatically. Since plus (` + `) is
>also used as in string concatenation, ` '1' + 1 ` is equal to ` '11' `.
>The string determines what ` + ` does. To overcome this, first convert the
>string to a number. For example: ` +varname ` or ` Number(varname) ` or
>` parseInt(varname, 10) ` or ` parseFloat(varname) `.
^^^^^^^^^^^^^^^^^^^^^
Quote:
Quote:
>[...]
just use parseInt:
>
alert(1 + parseInt('1'));
You should read texts more carefully (always a good strategy, but especially
on Usenet), and search the newsgroup's archives on how to use parseInt()
properly. (Incidentally, the text version of this FAQ entry should be
reformatted so that the important points are not overlooked that easily.)

And please trim your quotes to a reasonable minimum, as you can see here.
(That is also explained in FAQ 2.3.)


HTH

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles