Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / April 2007



Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
FAQ server - 29 Apr 2007 00:00 GMT
-----------------------------------------------------------------------
FAQ Topic - Why does 1+1 equal 11? or How do I convert a
string to a number?
-----------------------------------------------------------------------

Javascript variables are loosely typed: 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 deciding what + does. To overcome this, first convert the
string to a number. For example:
` Number(varname) ` or ` varname*1 ` or ` varname-0 ` or
` parseInt(varname, 10) ` or ` parseFloat(varname) ` or ` +varname `.
Prompt and form control values are strings, as is the result from
a prompt window. Convert these to numbers before performing
addition.

Additional Notes:

http://www.jibbering.com/faq/faq_notes/type_convert.html

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/1
2a450e5-4818-4a09-9878-cd7c6cd2a248.asp


===
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.
Dr J R Stockton - 29 Apr 2007 20:59 GMT
In comp.lang.javascript message <4633d1f8$0$90274$14726298@news.sunsite.
dk>, Sat, 28 Apr 2007 23:00:01, FAQ server <javascript@dotinternet.be>
posted:

> ...
> To overcome this, first convert the
>string to a number. For example:
>` Number(varname) ` or ` varname*1 ` or ` varname-0 ` or
>` parseInt(varname, 10) ` or ` parseFloat(varname) ` or ` +varname `.

That does not IMHO represent the present views of the newsgroup; the
following should do so better.

To overcome this, first convert the string to a number. For example:
` +varname ` ` or ` Number(varname) ` or
` parseInt(varname, 10) ` or ` parseFloat(varname).

While it may sometimes useful to describe inferior code, that should not
be done within a recommendation.

It's a good idea to read the newsgroup c.l.j and its FAQ.  See below.

Signature

(c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<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.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.