Faster, Higher, Stronger
更快,更高,更强

Number system conversion(进制转换)

If you want to convert a number from decimal system to binary system, you can do conversion like the picture shown below:

Corresponding code is like below:

function convertNum (originNum, base) {
    let quotient = originNum
    let remainer
    let arr = []
    while (quotient !== 0) {
        remainer = quotient % base // 取余
        quotient = Math.floor(quotient / base) // 取商
        arr.unshift(remainer)
    }
    return arr.join('')
}

 

赞(1) 打赏
未经允许不得转载:峰间的云 » Number system conversion(进制转换)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏