Tech/JavaScript
Javascript Comma
Enrai
2016. 2. 15. 10:47
1. 콤마 찍기
1 2 3 | function numberWithCommas(x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } | cs |
또는
1 2 3 | function comma(str) { return str.toString().replace(/(\d)(?=(?:\d{3})+(?!\d))/g, '$1,'); } | cs |
2. 콤마풀기
1 2 3 | function uncomma(str) { return str.toString().replace(/[^\d]+/g, ''); } | cs |
3. input box에서 사용자 입력시 바로 콤마를 찍어주기
1 2 3 4 5 | function inputNumberFormat(obj) { obj.value = comma(uncomma(obj.value)); } //<input type="text" onkeyup="inputNumberFormat(this)" /> | cs |
출처
http://blog.munilive.com/javascript-comma-uncomma/