commify
javascript 에서 숫자에 ',' 를 3자리 마다 넣어주는 방법ref. 1 에서 가져왔다.
d = 342432432432432
> 342432432432432
d.toString().replace(/(\d)(?=(\d{3})+$)/g, '$1,');
> "342,432,432,432,432"
d.toString().replace(/(\d)(?=(\d{3})+$)/, '$1,');
> "342,432432432432"
소수 둘째 자리에서 반올림
/**
Usage: currencyFormatted(12345.678);
result: 12345.68
@ref : http://www.queness.com/post/9806/5-missing-javascript-number-format-functions
**/
Utils.currencyFormatted = function(amount) {
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
s = minus + s;
return s;
}
댓글 없음:
댓글 쓰기