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: David Mark on 31 May 2010 10:44 On May 31, 5:12 am, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > On May 30, 6:28 pm, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk> > wrote: > > > (...) > > Opera 10.10, but not Opera 10.53 : Number.toString(radix) ignores > > radix. > > There's a bug in Safaris also: > Also? It's been demonstrated that there is no such bug in Opera 10.10.
From: Ry Nohryb on 31 May 2010 10:55 On May 31, 4:14 pm, "Evertjan." <exjxw.hannivo...(a)interxnl.net> wrote: > Ry Nohryb wrote on 31 mei 2010 in comp.lang.javascript: > > > /* get the digits that are valid for this base */ > > validDigits= "0123456789abcdefghijklmnopqrstuvwxyz".substr(0, base); > > nice! > > > /* validate structure and contents of the input str : */ > > /* ^ (optional) whitespace + (optional) a single char [-+] */ > > /* + (non-optional) 1 or more validDigits + (optional) a point */ > > /* + (optional) more validDigits + (optional) whitespace $ */ > > n= "^\\s{0,}([-+]{0,1})(["+ validDigits+ "]{1,})[.]{0,1}(["+ > > validDigits+ "]{0,})\\s{0,}$"; > > This would fail for 'pointfractions', like ".56ab" and "-.2" > > In short: > > n='^\\s*([-+]?)(['+validDigits+']+)\\.?(['+validDigits+']?)\\s*$'; > > However, to allow for such pointfractions would need something like this: > > n1='^\\s*([-+]?)(['+validDigits+']+)\\.?(['+validDigits+']?)\\s*$'; > n2='^\\s*([-+]?)()\\.(['+validDigits+']?)\\s*$'; > > n = n1 + '|' + n2 > > or > > n1 = '^\\s*([-+]?)' > n2a = '(['+validDigits+']+)\\.?' > n2b = '()\\.' > n3 = '(['+validDigits+']?)\\s*$'; > > n = n1 + '(?:' + n2a + '|' + n2b + ')' + n3 > > not tested. Hmmm, I think I just have to make that part optional in the regexp ({0,} instead of {1,}), and guard n[2] with || "0" in the call to parseInt() : String.prototype.toFP= function (base, n, r, w, div) { //20100531 by jorge(a)jorgechamorro.com /* check that base is in range and an integer */ if ((base < 2) || (base > 36) || (base % 1)) return NaN; /* get the digits that are valid for this base */ validDigits= "0123456789abcdefghijklmnopqrstuvwxyz".substr(0, base); /* validate structure and contents of the input str : */ /* ^ (optional) whitespace + (optional) a single char [-+] */ /* + (non-optional) 1 or more validDigits + (optional) a point */ /* + (optional) more validDigits + (optional) whitespace $ */ n= "^\\s{0,}([-+]{0,1})(["+ validDigits+ "]{0,})[.]{0,1}(["+ validDigits+ "]{0,})\\s{0,}$"; /* exec n on 'this' now, case-insensitively, and reuse n*/ if (!(n= new RegExp(n, "i").exec(this))) return NaN; /* got captured : */ /* n[1]= sign, n[2]=integer part, n[3]= fractional part */ if (isFinite(r= parseInt(n[2] || "0", base)) && (w= n[3].length)) { /* trim until div is finite */ while (!isFinite(div= Math.pow(base, w))) w--; r+= parseInt(n[3].substr(0, w), base)/ div; } /* sign is one of "1" or "+1" or "-1" */ return (n[1]+ "1")* r; }; "-.8".toFP(10) --> -0.8 -- Jorge.
From: Stefan Weiss on 31 May 2010 11:16 On 31/05/10 16:55, Ry Nohryb wrote: > On May 31, 4:14 pm, "Evertjan." <exjxw.hannivo...(a)interxnl.net> wrote: >> This would fail for 'pointfractions', like ".56ab" and "-.2" .... > Hmmm, I think I just have to make that part optional in the regexp > ({0,} instead of {1,}), and guard n[2] with || "0" in the call to > parseInt() : Since you like short code: {0,} is the same as * {1,} is the same as + :) -- stefan
From: Evertjan. on 31 May 2010 12:54 Stefan Weiss wrote on 31 mei 2010 in comp.lang.javascript: > Since you like short code: > > {0,} is the same as * > {1,} is the same as + and {0,1} is ? -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
From: Ry Nohryb on 31 May 2010 13:07
On May 31, 5:16 pm, Stefan Weiss <krewech...(a)gmail.com> wrote: > > Since you like short code: > > {0,} is the same as * > {1,} is the same as + > > :) Yes, but, don't you find their -the regExps'- syntax sufficiently confusing and unintelligible enough without the shortcuts ? Whose sick mind invented such an ugly thing ? :-) -- Jorge. |