Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder1.xlned.com!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <1495886.W2LQtbraro@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Sun, 18 Nov 2012 01:02:06 +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+&. <1620256.BhNbSm6C5H@PointedEars.de> <2KWdnfGCg9LknDXNnZ2dnUVZ_o2dnZ2d@westnet.com.au> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 58 NNTP-Posting-Date: 18 Nov 2012 01:02:06 CET NNTP-Posting-Host: f9ce8ea9.newsspool4.arcor-online.net X-Trace: DXC=JLE?l:HOlbUFm0Y?OE@2^X4IUKKP1Ae>Jg On 18/11/2012 1:37 AM, Thomas 'PointedEars' Lahn wrote: >> Andrew Poulos wrote: >>> On 17/11/2012 4:12 AM, 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. ^^^^^^^^^^^^^^^^^^^^^^^ >>> >>> How about: >>> >>> var s= "11."; var i = parseInt(s, 10); >> >> This will convert "11." to a Number value, stopping at `.'. However, >> the OP wanted decimals to be preserved. >> >>> var f = parseFloat(s); >> >> This will convert "11." to a Number value; preserving decimals, if >> any. >> >>> s = (i == f ? i : f) + ""; >> >> So there is no point in this with regard to the question. > In my testing with > > window.alert(parseFloat("11.")); > window.alert(parseFloat("11.0")); > window.alert(parseFloat("11.00")); > window.alert(parseFloat("11.000")); > window.alert(parseFloat("11.0000")); > > "11" was always returned ie. if the decimal part of the number is "zero" > then its not preserved. Which is exactly what the OP was asking for, so s = parseFloat(s) + ""; is sufficient [although I would prefer String(parseFloat(s)) or skip the explicit conversion altogether], and you can skip the variable declarations and initializations for `i' and `f'. PointedEars -- Sometimes, what you learn is wrong. If those wrong ideas are close to the root of the knowledge tree you build on a particular subject, pruning the bad branches can sometimes cause the whole tree to collapse. -- Mike Duffy in cljs,