473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Nov 1 '08 #1
1 1731
On Nov 1, 1:00*am, "FAQ server" <javascr...@dotinternet.bewrote:
-----------------------------------------------------------------------
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 `.
Unless the string contains a number whose base !== 10, (in which case
you'd use parseInt(string, base)), wouldn't it be better to clearly
recommend using the (+string) form as the best, as the one that will
yield the less surprising results, most of the times ?

Because it's less surprising to find out that (+"10px") yields NaN,
than that parseInt("012345e6") yields 5349, or even that
parseInt("12345e6", 10) yields 12345, or that parseFloat("") yields
NaN.

Although some may prefer parseFloat().

If not, I think it would be a good idea to at least elaborate a little
bit further on the topic.

But that’s just my "tuppence".
--
Jorge.
Nov 1 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Allerdyce.John | last post by:
Do I need to convert string to integer in python? or it will do it for me (since dynamic type)? In my python script, I have this line: x /= 10; when i run it, I get this error: TypeError:...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ 3.5. - How do I convert a Number into a String with exactly 2 decimal places?...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
11
by: davidj411 | last post by:
i am parsing a cell phone bill to get a list of all numbers and the total talktime spend on each number. i already have a unique list of the phone numbers. now i must go through the list of...
10
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
4
subedimite
by: subedimite | last post by:
Here I am with another question being a very new VBA user. I have this scenario to work with: I would think this is fairly common routine of work for VBA. I have a string of hex contents as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.