레이블이 javascript Amount of paste comma인 게시물을 표시합니다. 모든 게시물 표시
레이블이 javascript Amount of paste comma인 게시물을 표시합니다. 모든 게시물 표시

2012년 3월 30일 금요일

JAVASCRIPT Amount of paste comma, 천단위 콤마붙이기


금액에 천단위 콤마붙이기(javascript)

Amount of paste comma(javascript)

숫자형(Number)

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)

String.prototype.format = function(){    
 var num = parseFloat(this);    
 if( isNaN(num) ) return "0";      
 return num.format();
};  


exec)
var aa = 987654321
     ,aa = aa.format();
console.log(aa);

result)
==> 987,654,321