1. 程式人生 > >// 如何將浮點數點左邊的數每三位添加一個逗號,如12000000.11轉化為『12,000,000.11』?

// 如何將浮點數點左邊的數每三位添加一個逗號,如12000000.11轉化為『12,000,000.11』?

put repl AC 浮點 0.11 cti function str pan

function commafy(num){
  return num && num
    .toString()
    .replace(/(\d)(?=(\d{3})+\.)/g, function($1, $2){
      return $2 + ‘,‘;
    });
}

console.log(commafy(1200000123123.223))
let milliFormat = (input) => {
  return input && input.toString()
      .replace(/(^|\s)\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ‘,‘))
}

console.log(milliFormat(
1200000123123.223))

// 如何將浮點數點左邊的數每三位添加一個逗號,如12000000.11轉化為『12,000,000.11』?