JavaScript
// Convert decimal into a base between 2 and 64
// modified from http://stackoverflow.com/questions/656941/converting-decimals-to-sexagesimal-base-sixty-in-javascript
var decToBase = function() {
var decToBaseMap = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'Y', 'Z',
'+', '/'];
return function(number, base) {
if (base < 2 || base > 64) {
return "#base should be between 2 and 64#";
}
var negative = '';
if (number < 0) {
negative = '-';
}
number = number.toString().split('.');
var integer = Math.abs(number[0]);
var fraction = number[1];
var result = '';
do {
result = decToBaseMap[integer % base] + result;
integer = parseInt(integer / base, 10);
} while (integer > 0);
if (fraction) {
var decimalPlaces = fraction.toString().length;
result += '.';
fraction = parseFloat('.' + fraction);
var x = 0;
do {
x++;
var res = (fraction * base).toString().split('.');
result = result + decToBaseMap[res[0]];
if (res[1]) {
fraction = parseFloat('.' + res[1]);
}
else {
break;
}
} while (x < decimalPlaces);
}
return negative + result;
};
}();
var num = 2018;
for (var base = 2; base <= 64; base++) {
document.write("<div>Happy " + num + " or <span style=\"color: blue\">" + decToBase(num, base) + "</span> in base " + base + " !</div>");
}