SOURCE

/**
 * 整数部分
 */
function dectobin(number) {
    const result = [];
    let s = number;
    while(s > 0) {
        const y = s % 2;
        s = Math.floor(s / 2);
        result.push(y);
    }
    return result.reverse().join('');
}

/**
 * 小数部分
 */
function decptobin(number, max) {
    const result = [];
    let s = number;
    let len = 0;
    while(s > 0) {
        const y = s * 2;
        if (y >= 1) {
          result.push(1);
        } else {
          result.push(0);
        }
        if (y >= 1) {
            s = y - 1;
        } else {
            s = y;
        }
        len ++;
        if (len == max) {
            break;
        }
    }
    return result.join('');
}

console.log(decptobin(0.254, 24));
console 命令行工具 X clear

                    
>
console