Possible Duplicate:
How to format numbers using javascript?
我想在 JavaScript 中为数字添加千位分隔符,例如 .
。
1000 -> 1.000 1000000 -> 1.000.000
最优雅的方法是什么?
最佳答案
我不知道优雅...
function addCommas(n){
var rx= /(\d+)(\d{3})/;
return String(n).replace(/^\d+/, function(w){
while(rx.test(w)){
w= w.replace(rx, '$1,$2');
}
return w;
});
}
addCommas('123456789456.34');
返回值:(字符串)123,456,789,456.34
https://stackoverflow.com/questions/9743038/