본문 바로가기

자바스크립트

은근 많이 쓰이는 천 단위 콤마

728x90

원인이나 세세한 설명보다는 바로 쓸 수 있는 기술을 지향합니다.

 

1.toLocaleString()

숫자. toLocaleString()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

 

Number.prototype.toLocaleString() - JavaScript | MDN

The toLocaleString() method returns a string with a language-sensitive representation of this number. In implementations with Intl.NumberFormat API support, this method simply calls Intl.NumberFormat.

developer.mozilla.org

2. 함수로 만들기 

const displayComma = (num: number) => {
 return Number(num).toLocaleString() || ''
}

 

 

 

복사해서 가져다 쓰세요.

728x90