What is the best way to convert decimal numbers (base ten) to sexagesimal (base sixty) represented as a string using 0-9, A-Z and a-x as the digits.
I'm planning to code it in javascript but any help is appreciated.
Usage Example:
>>decToSex(60);
"10"
>>decToSex(123);
"23"
>>decToSex(1000000);
"4bke"
>>decToSex(1234567.89);
"5gu7.rO"
The final code I used, based on Paolo's answer:
var decToSex = function(){
var decToSexMap = ['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'];
return function(number){
var negative = '';
if (number < 0){
var negative = '-';
}
number = number.toString().split('.');
var integer = Math.abs(number[0]);
var fraction = number[1];
var result = '';
do {
result = decToSexMap[integer % 60] + result;
integer = parseInt(integer / 60);
} while (integer > 0);
if (fraction){
var decimalPlaces = fraction.toString().length;
result += '.';
fraction = parseFloat('.' + fraction);
var x = 0;
do {
x++;
var res = (fraction * 60).toString().split('.');
result = result + decToSexMap[res[0]];
if (res[1]) {
fraction = parseFloat('.' + res[1]);
}
else {
break;
}
} while (x < decimalPlaces);
}
return negative + result;
}
}();