Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17267 > unrolled thread

how to remove trailing decimal or decimal 0 of a string?

Started byskrite <shawn@skrite.net>
First post2012-11-16 09:12 -0800
Last post2012-11-17 21:08 +0000
Articles 4 on this page of 24 — 9 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#17315

FromChristoph Becker <cmbecker69@gmx.de>
Date2012-11-20 02:10 +0100
Message-ID<k8el8d$l71$1@speranza.aioe.org>
In reply to#17287
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).

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.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#17322

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-11-20 21:58 +0100
Message-ID<4089508.iS5gy5HxYY@PointedEars.de>
In reply to#17315
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>

[toc] | [prev] | [next] | [standalone]


#17329

FromDr J R Stockton <reply1247@merlyn.demon.co.uk.invalid>
Date2012-11-21 19:09 +0000
Message-ID<nCPP7hGibSrQFwmv@invalid.uk.co.demon.merlyn.invalid>
In reply to#17315
In comp.lang.javascript message <k8el8d$l71$1@speranza.aioe.org>, Tue,
20 Nov 2012 02:10:31, Christoph Becker <cmbecker69@gmx.de> posted:

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

But +myString is entirely safe, by definition, where myString looks like
a decimal number.  One just needs to be sure that the preceding
character is not a +, AFAIR; I would always use whitespace or = there.

Using anything other conversion, except where necessary, is either mere
bloat or pandering to uninformed and unintelligent readers.

Newbies should note that is often unwise to use ANY of those methods on
a string directly typed in to a control without first checking the
format of the string with a Regular Expression; and that, after suitable
checking and in ordinary circumstances, any of the methods will work
well.

-- 
 (c) John Stockton, nr London, UK.   E-mail, see Home Page.    Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

[toc] | [prev] | [next] | [standalone]


#17284

FromDr J R Stockton <reply1246@merlyn.demon.co.uk.invalid>
Date2012-11-17 21:08 +0000
Message-ID<TcNicRLhz$pQFwhB@invalid.uk.co.demon.merlyn.invalid>
In reply to#17267
In comp.lang.javascript message <e89247f7-aa7b-4c64-9de5-265ab78a5336@go
oglegroups.com>, Fri, 16 Nov 2012 09:12:49, skrite <shawn@skrite.net>
posted:

>Javascript: Hey all, if myString = "11." i would like it to be just "11"
>if it is "11.0" i would also like it to just be "11"
>however, if the string ends with anything else, like "11.3" or "11.34"
>i would like to leave it the way it is. Basically, i want to remove the
>decimal point and trailing 0 of any of my strings that are whole
>numbers.

S = "11." // etc.
S = S.replace(/\.0*$/, "")      // should work in all JS systems.

But remember that JavaScript numbers are floats, so do that after
rounding to the desired resolution.

IMHO, it is better practice to represent all numbers of a "set" with the
same number of decimal places, and never to have a decimal point which
is not surrounded by digits.

See the book described in <http://www.iupap.org/commissions/c2/redbook/>
- in my printed 1987 copy, it is section 1.3.2.

<http://fias.uni-frankfurt.de/~hees/tmp/iupap-symbols-typography.pdf>
appears to be a copy of a similar but slightly different document; but
its 1.3.2 matches.

-- 
 (c) John Stockton, near London.                Mail ?.?.Stockton@physics.org
  Web  <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, and links.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.javascript


csiph-web