숫자에 콤마와 언콤마를 편하게
// 숫자 타입에서 쓸 수 있도록 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;
};
// 문자열 타입에서 쓸 수 있도록 format() 함수 추가
String.prototype.format = function(){
var num = parseFloat(this);
if( isNaN(num) ) return "0";
return num.format();
};
// 콤마가 들어간 문자열을 콤마 제거하면서 숫자형 반환
String.prototype.unformat = function(){
var str = this.replace(/,/g,'');
var num = parseFloat(str);
if( isNaN(num) ) {
return "0";
}
return String(num);
};
// 숫자 타입 test
var num = 123456.012;
console.log(num.format()); // 123,456.012
num = 13546745;
console.log(num.format()); // 13,546,745
// 문자열 타입 test
console.log("12348".format()); // 12,348
console.log("12348.6456".format()); // 12,348.6456
출처: http://stove99.tistory.com/113