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: Benjamin 'BeRo' Rosseaux on 20 Jun 2010 20:32 Here my naive but secure variant, which works at least in V8 and in my own ECMAScript engine BESEN: String.prototype.toNumber=function(radix){ radix=(radix!==undefined)?(+radix):10; if((radix<1)||(radix>36))throw new RangeError("Bad radix"); var s=(""+this).toLowerCase(),whitespaces=" \t\r\n",signs="-+"; var n="0123456789abcdefghijklmnopqrstuvwxyz".substr(0,radix),intValue=0,fracValue=0,signValue=1,fracFactor=1,i,j=s.length,c,v; for(i=0;(i<j)&&(whitespaces.indexOf(c=s.charAt(i))>=0);i++); for(;(i<j)&&(signs.indexOf(c=s.charAt(i))>=0);i++)signValue*=(c=="-")?(-1):1; for(;(i<j)&&(whitespaces.indexOf(c=s.charAt(i))>=0);i++); for(;(i<j)&&((v=n.indexOf(c=s.charAt(i)))>=0);intValue=(intValue*radix)+v,i++); for(i+=(c==".")?1:(j+1);(i<j)&&((v=n.indexOf(c=s.charAt(i)))>=0);fracValue+=v*(fracFactor/=radix),i++); return(((intValue+fracValue)*(((c=="e")||(c=="p"))?Math.pow(radix,parseInt(s.substr(++i))):1.0))*signValue); } var a=Math.PI.toString(36); alert(a.toNumber(36));
From: Scott Sauyet on 21 Jun 2010 15:51 Dr J R Stockton wrote: > Consider putting the test button somewhere that the > "value" does not cover when showing a list, and putting > the results below, rather than beside, the input. I don't understand the first part of that. What browser has the button covering something important? Does the latest version fix the UI versions you mention? <http://scott.sauyet.com/Javascript/Test/2010-06-21a/> -- Scott
From: Scott Sauyet on 21 Jun 2010 15:55 On Jun 20, 8:32 pm, Benjamin 'BeRo' Rosseaux <benja...(a)rosseaux.com> wrote: > Here my naive but secure variant, which works at least in V8 and in my > own ECMAScript engine BESEN: > > String.prototype.toNumber=function(radix){ > [ ... ] I've included it on my latest version: <http://scott.sauyet.com/Javascript/Test/2010-06-21a/> Is it intentional that this accepts such input as "---+-+-1.234"? -- Scott
From: Benjamin 'BeRo' Rosseaux on 21 Jun 2010 16:43 Am 21.06.2010 21:55, schrieb Scott Sauyet: > Is it intentional that this accepts such input as "---+-+-1.234"? Yes, it accepts even "1.0e4" "1.0e-3" "1.0e+4" (base 10), "1f.1fp4" "1f.1fp+2" "1f.1fp-4" (base 16) and so on as exponent stuff, because Number.prototype.toString "can" produce so such results, at least in BESEN and even some other engines.
From: Scott Sauyet on 21 Jun 2010 16:50
Benjamin 'BeRo' Rosseaux wrote: > Am 21.06.2010 21:55, schrieb Scott Sauyet: > > > Is it intentional that this accepts such input as "---+-+-1.234"? > > Yes, it accepts even "1.0e4" "1.0e-3" "1.0e+4" (base 10), "1f.1fp4" > "1f.1fp+2" "1f.1fp-4" (base 16) and so on as exponent stuff, because > Number.prototype.toString "can" produce so such results, at least in > BESEN and even some other engines. I saw the exponent code, and am not quite sure how that should play with bases higher than 14 (it gets tricky when you consider that "1.0e3", base 10, should convert to 1000 but "1.0e3", base 16, should convert to 1.055419921875. The question is what does "e" represent? But do you foresee a need for multiple consecutive minus/plus signs? -- Scott |