Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting decimals to sexagesimal (base sixty) in javascript

Tags:

javascript

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;
    }
}();
like image 539
AnnanFay Avatar asked Mar 18 '09 04:03

AnnanFay


1 Answers

This works with the test data provided, but you probably wanna run a few more tests on it:

<script>
var convert = new Array(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');

function decToSex(num) {
    var extract = num.toString().split('.');
    num = extract[0];
    var pieces = new Array();
    do {
        pieces.push(convert[num % 60]);
        num = parseInt(num/60);
    } while (num > 0);
    pieces = pieces.reverse();
    var rem = extract[1];
    if(rem) {
        rem = parseFloat('.' + rem);
        var x = 0;
        var dec = new Array();
        do {
            x++;
            var res = (rem * 60).toString().split('.');
            dec.push(convert[res[0]]);
            if(res[1]) {
                rem = parseFloat('.' + res[1]);
            } else {
                break;
            }
        } while (x < 3); // work up to 3 decimal places, change for more.
    }
    var myResult = pieces.join('');
    if(dec) {
        myResult += '.' + dec.join('');
    }
    return myResult;
}

alert(decToSex(60));
alert(decToSex(123));
alert(decToSex(1000000));
alert(decToSex(1234567.89));
</script>
like image 73
Paolo Bergantino Avatar answered Oct 06 '22 00:10

Paolo Bergantino