Nodejs Number to Base Convert toBase(base)

Here you can find the source of toBase(base)

Method Source Code

"use strict";/*from w  w  w  . ja  v a  2  s .  c  o  m*/

Number.prototype.toBase = function (base) {
    var symbols = 
    "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
    var decimal = this;
    var conversion = "";

    if (base > symbols.length || base <= 1) {
        return false;
    }

    while (decimal >= 1) {
        conversion = symbols[(decimal - (base * Math.floor(decimal / base)))] + 
                     conversion;
        decimal = Math.floor(decimal / base);
    }

    return (base < 11) ? parseInt(conversion) : conversion;
}

Related

  1. toBijectiveBase26( ()
    Number.prototype.toBijectiveBase26 = (function () {
      return function toBijectiveBase26() {
        n = this
        ret = "";
        while(parseInt(n)>0){
          --n;
          ret += String.fromCharCode("A".charCodeAt(0)+(n%26));
          n/=26;
        return ret.split("").reverse().join("");
      };
    }());
    
  2. base(b,c)
    Number.prototype.define("base", function(b, c) {
      var s = "", n = this
      if (b > (c = (c || "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz").split("")).length || b < 2) {
        return ""
      while (n) {
        s = c[n % b] + s, n = Math.floor(n / b)
      return s
    ...