숫자에 천단위로 콤마(,) 찍기
정규표현식을 이용하여 숫자에 천단위로 콤마를 삽입하는 실례입니다.
[code]
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
[/code]
또는
[code]
// 숫자 타입에서 쓸 수 있도록 format() 함수 추가
Number.prototype.format = function(){
if(this==0) return 0;
var reg = /(^[+-]?\d+)(\d{3})/;
var n = (this + '');
while (reg.test(n)) n = n.replace(reg, '$1' + ',' + '$2');
return n;
};
console.log(Number(1234567).format()); // 1,234,567
[/code]
개발에 좋은 팁이 되길 바랍니다,
댓글 11개
jihan6?
2년 전
new Intl.NumberFormat().format(1234567);
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat
swallow
2년 전
@jihan6? 자바스크립트에 디폴트 함수가 있었네요, 단, 브라우저를 좀 가리는듯합니다,.
SimpleCode
2년 전
@jihan6? 저 같은 경우에는 Intl 객체가 있으면 그걸 쓰고, 없는 환경에서만 fallback으로 쓰고 있습니다.
2년 전
@SimpleCode polyfill 사용이 유용하죠.
https://formatjs.io/docs/polyfills/
https://formatjs.io/docs/polyfills/
2년 전
@jihan6?
우리나라 거는 없네요.
우리나라 거는 없네요.
jihan?
2년 전
@엑스엠엘 ???
const number = 123456.789;
console.log(new Intl.NumberFormat('ko-KR').format(number));
// 아무것도 지정하지 않으면 사용자 브라우저 설정에 따라 자동 처리
console.log(new Intl.NumberFormat().format(number));
const number = 123456.789;
console.log(new Intl.NumberFormat('ko-KR').format(number));
// 아무것도 지정하지 않으면 사용자 브라우저 설정에 따라 자동 처리
console.log(new Intl.NumberFormat().format(number));
2년 전
@jihan?
우리나라는 네 자리마다(만 단위) 끊어 줘야
읽기가 좋습니다.
우리나라는 네 자리마다(만 단위) 끊어 줘야
읽기가 좋습니다.
jihan?
2년 전
@엑스엠엘
말씀하시는 “우리나라”가 어느 나라길래 네자리씩 끊죠?
const number = 123456.789
console.log(new Intl.NumberFormat('ko-KR').format(number));
console.log(new Intl.NumberFormat().format(number));
output
> "123,456.789"
> "123,456.789"
이것과 다르게 나오나요?
말씀하시는 “우리나라”가 어느 나라길래 네자리씩 끊죠?
const number = 123456.789
console.log(new Intl.NumberFormat('ko-KR').format(number));
console.log(new Intl.NumberFormat().format(number));
output
> "123,456.789"
> "123,456.789"
이것과 다르게 나오나요?
2년 전
@jihan?
표현이 좀 부족했네요.
우리나라 말에 맞은 표현(포맷)이라고 했어야 했네요.
표현이 좀 부족했네요.
우리나라 말에 맞은 표현(포맷)이라고 했어야 했네요.
니얼바이웹
2년 전
그냥
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
이렇게만 해도 ...되지 않나요.
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
이렇게만 해도 ...되지 않나요.
2년 전
감사합니다. 工作一结束手机就销毁。
게시판 목록
개발자팁
개발과 관련된 유용한 정보를 공유하세요.
질문은 QA에서 해주시기 바랍니다.
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4766 | jQuery | 8년 전 | 1674 | ||
| 4765 | jQuery | 8년 전 | 1349 | ||
| 4764 | jQuery | 8년 전 | 2164 | ||
| 4763 | jQuery | 8년 전 | 1853 | ||
| 4762 | jQuery | 8년 전 | 1626 | ||
| 4761 | jQuery | 8년 전 | 1932 | ||
| 4760 | jQuery | 8년 전 | 1297 | ||
| 4759 | jQuery | 8년 전 | 1596 | ||
| 4758 | jQuery | 8년 전 | 1310 | ||
| 4757 | jQuery | 8년 전 | 1273 | ||
| 4756 | jQuery | 8년 전 | 1496 | ||
| 4755 | jQuery | 8년 전 | 1646 | ||
| 4754 | jQuery | 8년 전 | 1612 | ||
| 4753 | jQuery | 8년 전 | 1766 | ||
| 4752 | jQuery | 8년 전 | 1519 | ||
| 4751 | jQuery | 8년 전 | 1838 | ||
| 4750 | jQuery | 8년 전 | 1981 | ||
| 4749 | jQuery | 8년 전 | 1440 | ||
| 4748 | PHP | 8년 전 | 2809 | ||
| 4747 | jQuery | 8년 전 | 1390 | ||
| 4746 | jQuery | 8년 전 | 1645 | ||
| 4745 | jQuery | 8년 전 | 1653 | ||
| 4744 | jQuery | 8년 전 | 1533 | ||
| 4743 | jQuery | 8년 전 | 1612 | ||
| 4742 | jQuery | 8년 전 | 1704 | ||
| 4741 | jQuery | 8년 전 | 1597 | ||
| 4740 | jQuery | 8년 전 | 1605 | ||
| 4739 | jQuery | 8년 전 | 1797 | ||
| 4738 | jQuery | 8년 전 | 1306 | ||
| 4737 | jQuery | 8년 전 | 2097 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기