前端縮短數字的長度解決方案[10進制轉化爲64進制]

function string10to64 (number) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$'.split(''),
        radix = chars.length,
        qutient = +number,
        arr = [];
    do {
        mod = qutient % radix;
        qutient = (qutient - mod) / radix;
        arr.unshift(chars[mod]);
    } while (qutient);
    return arr.join('');
}

function string64to10(number_code) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$',
        radix = chars.length,
        number_code = String(number_code),
        len = number_code.length,
        i = 0,
        origin_number = 0;
    while (i < len) {
        origin_number += Math.pow(radix, i++) * chars.indexOf(number_code.charAt(len - i) || 0);
    }
    return origin_number;
}

//注意事項,因爲js是雙精度,10進制長度最多爲15位, 超過15位的數字不適用與本方法spa

var a = string10to64('2093890843211111') ;code

"7S67pw7Ld"string

var b = string64to10('7S67pw7Ld');it

"2093890843211111"io

相關文章
相關標籤/搜索