개발/JavaScript

javascript | 숫자 ,콤마(comma) 추가 정규식

AM0530 2019. 12. 4. 11:19

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

console.log(numberWithCommas(12346854562));

function numberWithCommas(x) {     
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
} 

console.log(numberWithCommas(12346854562));

출처 : https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript

 

How to print a number with commas as thousands separators in JavaScript

I am trying to print an integer in JavaScript with commas as thousands separators. For example, I want to show the number 1234567 as "1,234,567". How would I go about doing this? Here is how I am...

stackoverflow.com

 

2차 출처 : https://fruitdev.tistory.com/160