zl程序教程

您现在的位置是:首页 >  其它

当前栏目

[Algorithm] Convert a number from decimal to binary

to number from Binary ALGORITHM convert Decimal
2023-09-14 08:59:14 时间

125, how to conver to binary number?

 

function DecimalToDinary (n) {
  let temp = n;
  let list = [];

  if (temp <= 0) {
    return '0000';
  }

  while (temp > 0) {
    let rem = temp % 2;
    list.push(rem);
    temp = parseInt(temp / 2, 10);
  }

  return list.reverse().join('');
}

console.log(DecimalToDinary(125)) // 1111101