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

댓글 2개:

  1. Excellent way of telling, and nice paragraph to take facts concerning my presentation topic, which i am
    going to deliver in college.

    Here is my homepage; 3ds emulator

    답글삭제
  2. In fact when someone doesn't be aware of after that its up to
    other users that they will assist, so here it occurs.



    Have a look at my blog post: real racing 3 cheater; mskr.net,

    답글삭제