Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17272
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Denis McMahon <denismfmcmahon@gmail.com> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: how to remove trailing decimal or decimal 0 of a string? |
| Date | Sat, 17 Nov 2012 08:05:17 +0000 (UTC) |
| Organization | A noiseless patient Spider |
| Lines | 38 |
| Message-ID | <k87gft$gnb$2@dont-email.me> (permalink) |
| References | <e89247f7-aa7b-4c64-9de5-265ab78a5336@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 8bit |
| Injection-Date | Sat, 17 Nov 2012 08:05:17 +0000 (UTC) |
| Injection-Info | mx04.eternal-september.org; posting-host="986f797cbd3b8f473a4775d73e9098bc"; logging-data="17131"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1902QVBujsLCOSFStcYOgyGOWx5GslzBSs=" |
| User-Agent | Pan/0.136 (I'm far too busy being delicious; GIT 926a150 git://git.gnome.org/pan2) |
| Cancel-Lock | sha1:e1DM4/hWv/L1a1C4tTEt0WPSO5I= |
| Xref | csiph.com comp.lang.javascript:17272 |
Show key headers only | View raw
On Fri, 16 Nov 2012 09:12:49 -0800, skrite 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.
I did the following tests in the javascript console:
--
if (Math.floor(parseFloat("10.5")) == parseFloat("10.5")) Math.floor
(parseFloat("10.5")).toString(); else parseFloat("10.5").toString();
"10.5"
--
if (Math.floor(parseFloat("10.")) == parseFloat("10.")) Math.floor
(parseFloat("10.")).toString(); else parseFloat("10.").toString();
"10"
--
if (Math.floor(parseFloat("10")) == parseFloat("10")) Math.floor
(parseFloat("10")).toString(); else parseFloat("10").toString();
"10"
So assuming you're starting with a numeric value written as a string, and
you want to end up with a numeric value written as a string, something
like the following might work:
x = parseFloat( myString );
if ( Math.floor( x ) == x )
myString = Math.floor( x ).toString();
else
myString = x.toString();
Rgds
Denis McMahon
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