숫자에 천단위로 콤마(,) 찍기
정규표현식을 이용하여 숫자에 천단위로 콤마를 삽입하는 실례입니다.
[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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4946 | node.js | 6년 전 | 2616 | ||
| 4945 | node.js | 6년 전 | 2389 | ||
| 4944 | node.js | 6년 전 | 2526 | ||
| 4943 | node.js | 6년 전 | 2277 | ||
| 4942 | node.js | 6년 전 | 2259 | ||
| 4941 | node.js | 6년 전 | 2723 | ||
| 4940 | node.js | 6년 전 | 1872 | ||
| 4939 | node.js | 6년 전 | 1998 | ||
| 4938 | node.js | 6년 전 | 2461 | ||
| 4937 | node.js | 6년 전 | 2253 | ||
| 4936 | node.js | 6년 전 | 2327 | ||
| 4935 | node.js | 6년 전 | 2143 | ||
| 4934 | node.js | 6년 전 | 2453 | ||
| 4933 | node.js | 6년 전 | 2252 | ||
| 4932 | node.js | 6년 전 | 2692 | ||
| 4931 | node.js | 6년 전 | 2077 | ||
| 4930 | node.js | 6년 전 | 2002 | ||
| 4929 | node.js | 6년 전 | 8639 | ||
| 4928 | node.js | 6년 전 | 3757 | ||
| 4927 | node.js | 6년 전 | 2399 | ||
| 4926 | node.js | 6년 전 | 2509 | ||
| 4925 | node.js | 6년 전 | 2092 | ||
| 4924 | node.js | 6년 전 | 3380 | ||
| 4923 | node.js | 6년 전 | 2230 | ||
| 4922 | node.js | 6년 전 | 1996 | ||
| 4921 | node.js | 6년 전 | 2050 | ||
| 4920 | node.js | 6년 전 | 1769 | ||
| 4919 | node.js | 6년 전 | 2043 | ||
| 4918 | node.js | 6년 전 | 2184 | ||
| 4917 | node.js | 6년 전 | 2400 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기