Prev: Firefox cache issue
Next: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2010-05-27)
From: Ry Nohryb on 30 May 2010 19:12 On May 30, 10:15 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > > /* Tested in Safari, Chrome, Opera & FF */ > > String.prototype.toFP= function (base, n, r, w, div) { > if (/[^0-9a-z\.+-]/i.test(this)) return NaN; > n= this.split('.'); > if (isFinite(r= parseInt(n[0], base))) { > if (w= (n= n[1]).length) { > /*trim until it's finite*/ > while (!isFinite(div= Math.pow(base, w))) w--; > r+= (r<0 ? -1:1)* parseInt(n.substr(0, w), base)/ div; > } > } > return r; > }; ****** BUG FIX: /* Tested in Safari, Chrome, Opera & FF */ String.prototype.toFP= function (base, n, r, w, div, s) { if (/[^0-9a-z\.+-]/i.test(this)) return NaN; n= this.split('.'); if (isFinite(r= parseInt(n[0], base))) { if (n[1] && (w= n[1].length)) { /*trim until div is finite*/ while (!isFinite(div= Math.pow(base, w))) w--; /*Tests sign properly for -0.xxxx*/ s= (r || parseInt(n[0]+ "1", base)) < 0 ? -1:1; r+= s* parseInt(n[1].substr(0, w), base)/ div; } } return r; }; "ff".toFP(16) /* integer input no longer throws */ --> 255 (-Math.PI/10).toString(16).toFP(16) /* proper sign detection */ --> -0.3141592653589793 -- Jorge.
From: David Mark on 30 May 2010 19:49 On May 30, 12:28 pm, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk> wrote: > In comp.lang.javascript message <Xns9D87B7820AFF3eej...(a)194.109.133.242> > , Sat, 29 May 2010 16:02:20, Evertjan. <exjxw.hannivo...(a)interxnl.net> > posted: > > >Dr J R Stockton wrote on 28 mei 2010 in comp.lang.javascript: > > >> In comp.lang.javascript message <Xns9D85CE4464FCFeej...(a)194.109.133.242> > >> , Thu, 27 May 2010 18:16:34, Evertjan. <exjxw.hannivo...(a)interxnl.net> > >> posted: > >However we were [or at least I was] just contemplating a general function > >for conversion of any root[1..36] floating point string to number value. > > Remember to include isNaN & !isFinite testing. > > >Such functions are much safer explicit than embedded, > > Sometimes. The advantage of an embedded bug is that someone else is > more likely to find it first, and get it fixed. > > Opera 10.10, but not Opera 10.53 : Number.toString(radix) ignores > radix. Not from what I've seen (just tested it). I just happened to be working on some code that relies on toString to work with a radix and your comment was a real spit-take moment. Which Opera 10.10 are you testing?
From: Thomas 'PointedEars' Lahn on 30 May 2010 20:34 David Mark wrote: > Dr J R Stockton wrote: >> Opera 10.10, but not Opera 10.53 : Number.toString(radix) ignores >> radix. JFTR: Number.toString !== Number.prototype.toString. > Not from what I've seen (just tested it). I just happened to be > working on some code that relies on toString to work with a radix and > your comment was a real spit-take moment. > > Which Opera 10.10 are you testing? WFM, too. (128).toString(36) === "3k" in "Opera/9.80 (X11; Linux i686; U; en) Presto/2.2.15 Version/10.10" (Build 4742) and "Opera/9.80 (Windows NT 5.1; U; en) Presto/2.2.15 Version/10.10" (Build 1893). 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
From: David Mark on 30 May 2010 21:14 On May 30, 8:34 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > David Mark wrote: > > Dr J R Stockton wrote: > >> Opera 10.10, but not Opera 10.53 : Number.toString(radix) ignores > >> radix. > > JFTR: Number.toString !== Number.prototype.toString. Right. Number.toString === Function.prototype.toString > > > Not from what I've seen (just tested it). I just happened to be > > working on some code that relies on toString to work with a radix and > > your comment was a real spit-take moment. > > > Which Opera 10.10 are you testing? > > WFM, too. (128).toString(36) === "3k" in "Opera/9.80 (X11; Linux i686; U; > en) Presto/2.2.15 Version/10.10" (Build 4742) and "Opera/9.80 (Windows NT > 5.1; U; en) Presto/2.2.15 Version/10.10" (Build 1893). > I'd be pretty shocked if it didn't. Would be a hell of a regression.
From: Evertjan. on 31 May 2010 03:15
Dr J R Stockton wrote on 30 mei 2010 in comp.lang.javascript: >>btw: in root 1 only the zero value can be expressed. radix for Latinists? > Eh? How would you express 5 or 0.7 ? -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) |