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


Groups > comp.lang.javascript > #17287

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!feeder3.cambriumusenet.nl!feed.tweaknews.nl!193.141.40.65.MISMATCH!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="ISO-8859-1"
Message-ID <1788183.LAg6v8g3if@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Sun, 18 Nov 2012 01:56:31 +0100
User-Agent KNode/4.4.11
Content-Transfer-Encoding 7Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
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>
Followup-To comp.lang.javascript
MIME-Version 1.0
Lines 78
NNTP-Posting-Date 18 Nov 2012 01:56:32 CET
NNTP-Posting-Host f9ce8ea9.newsspool4.arcor-online.net
X-Trace DXC=7FinUkZH2?<kUFX=Y?aLP;4IUK<Cl32<14Fo<]lROoR18kF<OcfhCO;Qce:MKiA0>5DZm8W4\YJN<J`:FmdeL>K0maXD]mA^N40EdFnDWPO=`8
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:17287

Followups directed to: comp.lang.javascript

Show key headers only | View raw


BootNic wrote:

> skrite <shawn@skrite.net> wrote:
>> 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.
> 
> Perhaps convert the string to number:

You are a bit late to the party, but your posting gives me reason to expand 
on this:
 
> myString = Number(myString);

Contrary to popular belief, the different approaches to string-to-number 
conversion are _not_ equivalent:

Number(myString) will return NaN if there are any non-digits and non-dots in 
myString, ignoring leading whitespace.  It will parse (the value of) 
`myString' as a hexadecimal number if it starts (after leading whitespace) 
with `0x', and will return NaN if there are any non-hexadecimal digits in 
the string (including the decimal point).  It will _not_ convert `myString' 
to String if it is not one.

+myString will do the same as Number(myString).  The "Unary +" operator was 
introduced in the first Edition (1997) as was `Number', but it was not 
implemented before JavaScript 1.3 (1998) and JScript 3.1.3510 (1999).  
(CAVEAT: Per older, non-scientific research.  My current, overall more 
complete research [not yet online] did not include those implementations 
because of backwards compatibility issues; but I intend to fix that.)

parseInt(myString) will first convert `myString' to String(!) before parsing 
it as a number of unspecified base.  The base is determined by the first 
leading non-whitespace sequence: if it is `0x', the base is 16 
(hexadecimal), in older implementations (of Edition 3 and before) if it is 
`0' the base would be 8 (octal; in Ed. 2 mandatory, in Ed. 3 implementation-
dependent), otherwise 10 (decimal).  It will stop parsing before the first 
character that it cannot interpret according to the determined base.  For 
those reasons, parseInt() should not be called like this.

parseInt(myString, base) will first convert `myString' to String before 
parsing it as a number of the specified base, while ignoring `0x' (after 
leading whitespace) if base == 16.  It will only return NaN if no digits 
could be found before the first non-digit (ignoring leading whitespace).

parseFloat(myString) will first convert `myString' to String before parsing 
it as a decimal number, and it will return NaN if that is not possible.  
Leading whitespace will be ignored.  (The built-in parseFloat() cannot parse 
non-decimal fractions; see jsx.math.parseFloat() for that.)

Therefore, parseFloat(myString) is probably the best solution here.

The forced conversion of the argument value to String is also important 
because the string representation of Number values is implementation-
dependent.  Thus,

  parseFloat(n) === n

is not necessarily true for all Number values `n'.

> if it needs to be a string:
> 
> myString = String(Number(myString));

Same problem.  For defined (and potentially more exact) string 
representations of Number values, ECMAScript Ed. 3 to 5.1 specify 
Number.prototype.toFixed(), Number.prototype.toExponential(), and 
Number.prototype.toPrecision().


PointedEars
-- 
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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