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 31 May 2010 05:12 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: (Math.PI/10000).toString(anyBase) --> "0" -- Jorge.
From: Evertjan. on 31 May 2010 05:32 Ry Nohryb wrote on 31 mei 2010 in comp.lang.javascript: > 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; s= parseInt(n[0]+ '1', base)<0 ? -1:1; > r+= s* parseInt(n[1].substr(0, w), base)/ div; > } > } > return r; >}; > -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
From: Evertjan. on 31 May 2010 05:49 Evertjan. wrote on 31 mei 2010 in comp.lang.javascript: > Ry Nohryb wrote on 31 mei 2010 in comp.lang.javascript: > >> 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; > > > s= parseInt(n[0]+ '1', base)<0 ? -1:1; Or even: s= n[0][0]=='-' ?-1 :1; >> r+= s* parseInt(n[1].substr(0, w), base)/ div; >> } >> } >> return r; >>}; >> > > > -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
From: Stefan Weiss on 31 May 2010 07:05 On 31/05/10 09:15, Evertjan. wrote: > 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 ? In a unary system it's more usual to count like this: 1 11 111 1111 Not all values can be expressed this way (for example, 0 or 1.05), but at least you get more values that just the zero. -- stefan
From: Evertjan. on 31 May 2010 07:29
Evertjan. wrote on 31 mei 2010 in comp.lang.javascript: > Evertjan. wrote on 31 mei 2010 in comp.lang.javascript: > >> Ry Nohryb wrote on 31 mei 2010 in comp.lang.javascript: >> >>> String.prototype.toFP= function (base, n, r, w, div, s) { >>> if (/[^0-9a-z\.+-]/i.test(this)) return NaN; This test is not enough now, meseems: a = '12345abc.5def' document.write((a.toFP(10)) // 12345.0005 use: var re = base>10 ?'a-'+String.fromCharCode(86+base) :''; re = new RegExp('[^0-9'+re+'\.+-]','i'); if (re.test(this)) return NaN; and add: if (/[+-]/.test(this.substr(1))) return NaN; //+- must be in position 0 if (this.replace(/[^\.]+/g,'').length>1) return NaN; // count of . <=1 >>> 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; >> >> >> s= parseInt(n[0]+ '1', base)<0 ? -1:1; > > Or even: > > s= n[0][0]=='-' ?-1 :1; > >>> r+= s* parseInt(n[1].substr(0, w), base)/ div; >>> } >>> } >>> return r; >>>}; >>> >> >> >> > > > -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) |