Place commas in Javascript integers

后端 未结 4 501
南笙
南笙 2021-01-18 00:57

So I have a value in Javascript:

var val = Entry.val;

One example of this value is 277385. How do I, in Javascript, convert th

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-18 01:14

    I'm not sure why the other answers split the number on a decimal point- you can replace, starting with a digit,until there are no more digits. It will quit when it runs out of digits or hits a non-digit.

    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;
        });
    }
    

提交回复
热议问题