Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17322
| Message-ID | <4089508.iS5gy5HxYY@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-11-20 21:58 +0100 |
| Subject | Re: how to remove trailing decimal or decimal 0 of a string? |
| Newsgroups | comp.lang.javascript |
| References | <e89247f7-aa7b-4c64-9de5-265ab78a5336@googlegroups.com> <20121117112826.00530fe7@bootnic.server> <1788183.LAg6v8g3if@PointedEars.de> <k8el8d$l71$1@speranza.aioe.org> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
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, <f806at$ail$1$8300dec7@news.demon.co.uk>
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to remove trailing decimal or decimal 0 of a string? skrite <shawn@skrite.net> - 2012-11-16 09:12 -0800
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-17 07:10 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-17 13:51 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-17 15:37 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-18 08:28 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-18 01:02 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-18 15:40 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Denis McMahon <denismfmcmahon@gmail.com> - 2012-11-17 08:05 +0000
Re: how to remove trailing decimal or decimal 0 of a string? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-17 09:24 +0100
Re: how to remove trailing decimal or decimal 0 of a string? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-11-17 09:29 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Christoph Becker <cmbecker69@gmx.de> - 2012-11-17 12:04 +0100
Re: how to remove trailing decimal or decimal 0 of a string? BootNic <bootnic.bounce@gmail.com> - 2012-11-17 11:28 -0500
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-18 01:56 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Dr J R Stockton <reply1247@merlyn.demon.co.uk.invalid> - 2012-11-19 19:53 +0000
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-20 11:03 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-20 01:50 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Dr J R Stockton <reply1247@merlyn.demon.co.uk.invalid> - 2012-11-21 18:44 +0000
Re: how to remove trailing decimal or decimal 0 of a string? Andrew Poulos <ap_prog@hotmail.com> - 2012-11-22 14:07 +1100
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-20 01:04 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Dr J R Stockton <reply1247@merlyn.demon.co.uk.invalid> - 2012-11-21 19:00 +0000
Re: how to remove trailing decimal or decimal 0 of a string? Christoph Becker <cmbecker69@gmx.de> - 2012-11-20 02:10 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-20 21:58 +0100
Re: how to remove trailing decimal or decimal 0 of a string? Dr J R Stockton <reply1247@merlyn.demon.co.uk.invalid> - 2012-11-21 19:09 +0000
Re: how to remove trailing decimal or decimal 0 of a string? Dr J R Stockton <reply1246@merlyn.demon.co.uk.invalid> - 2012-11-17 21:08 +0000
csiph-web