The World of Remnant

数字转为千分位

每3位加逗号



var num = prompt('输入大数字', '25000000')

// 我写的
function douhao(num) {
  num = Array.from(num.toString())
  for (var i = num.length - 3; i > 0; i -= 3) {
    num.splice(i, 0, ',')
  }
  console.log(num.join(''));
}
// douhao(num)

// 参考答案
function toThousands(num) {
    var num = num.toString(),
     result = ''
    while (num.length > 3) {
        result = ',' + num.slice(-3) + result;
        num = num.slice(0, num.length - 3);
    }
    if (num) { result = num + result; }
    console.log(result);
}
// toThousands(num)