Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.karotte.org!uucp.gnuu.de!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <4089508.iS5gy5HxYY@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Tue, 20 Nov 2012 21:58:58 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. <20121117112826.00530fe7@bootnic.server> <1788183.LAg6v8g3if@PointedEars.de> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 62 NNTP-Posting-Date: 20 Nov 2012 21:58:58 CET NNTP-Posting-Host: 14d49e7f.newsspool4.arcor-online.net X-Trace: DXC=JSR[C:GZhmW[7Non7UCi8U4IUKKP?H3jTVH[5eTEbL6aNh]0f\ X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:17322 Christoph Becker wrote: > Thomas 'PointedEars' Lahn wrote: >> Contrary to popular belief, the different approaches to string-to-number >> conversion are _not_ equivalent: >> [...] >> Therefore, parseFloat(myString) is probably the best solution here. >> [...] > > Thank you very much for your most informative answer. :) It clearly > showed, that my uncertainty about using +myString was appropriate, and > that better solutions to the problem at hand do exist (obviously no > "perfect" one, though). You are welcome. > FWIW: I've noticed a /minor/ (as it's not related to the OP's question) > omission in your explanations: all(?) of the conversion routines will > recognize a leading sign (+/-) and will ignore trailing whitespace. That is a very good question. Actually, Number(" -0x20 "), Number(" +0x20 "), +" -0x20 ", and +" +0x20 " all result in NaN, while Number(" 0x20 ") and +" 0x20 " both result in 32, parseInt(" -0x20 ", 16) returns -32, and parseInt(" +0x20 ", 16) returns 32 (with and without specified base) in Chromium 22.0.1229.94 on GNU/Linux. That complies with ECMAScript Ed. 5.1, sections 9.3.1 and 15.1.2.2, and the following relevant productions in the former: | StringNumericLiteral ::: | StrWhiteSpace_opt | StrWhiteSpace_opt StrNumericLiteral StrWhiteSpace_opt | | […] | | StrNumericLiteral ::: | StrDecimalLiteral | HexIntegerLiteral | | […] | | HexIntegerLiteral ::: | 0x HexDigit | 0X HexDigit | HexIntegerLiteral HexDigit | | HexDigit ::: one of | 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F As for the trailing whitespace, though, for parseInt() and parseFloat() that is already covered by the explanation that parsing stops before the first character that does not belong to the determined or specified numeric literal [§15.1.2.2, 11.; §15.1.2.3, 4.]. 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,