Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17287
| Message-ID | <1788183.LAg6v8g3if@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-11-18 01:56 +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> |
| Followup-To | comp.lang.javascript |
Followups directed to: comp.lang.javascript
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
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