亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

javascript - How to add a comma to every 3 numbers in js recursively?
習(xí)慣沉默
習(xí)慣沉默 2017-05-19 10:36:04
0
8
851

For example, 234234 becomes 234,234. Seeking recursive implementation, the less code, the better.

習(xí)慣沉默
習(xí)慣沉默

reply all(8)
僅有的幸福

There is a super simple method 234234..toLocaleString()

Ty80

I guess you want to realize the digit display of numbers. You want "12345" to be displayed as "12,345" instead of "123,45". Some people got it wrong.
The solution given by some people is that the number "123456" will be output as ",123,456", which obviously has an extra comma. My method will not have this problem.

1L's answer (use NumberObject.toLocaleString(), convert the numbers to strings, and use the local number format sequence.) can solve the problem, but it is not recommended because it depends on your local preferences and the results may be different for different people.

Implemented in a non-recursive way

Also supports decimal points!

"12345".split('').reverse().join('').replace(/(\d{3})\B/g,',').split('').reverse().join('');  //輸出 12,345

"123456789".split('').reverse().join('').replace(/(\d{3})\B/g,',').split('').reverse().join(''); //輸出 123,456,789

"1234567.89".split('').reverse().join('').replace(/(\d{3})\B/g,',').split('').reverse().join(''); //輸出 1,234,567.89

Recursively implemented

This question requires recursive implementation, so the answer also provides recursive implementation.

//只處理整數(shù)
function addComma(str, currentIndex, result) {
    if (currentIndex === undefined) {
        currentIndex = str.length - 1;
    }
    if (result === undefined) {
        result = '';
    }
    if (currentIndex < 0) {
        return result;
    }
    result = str.charAt(currentIndex) + result;
    if ((str.length - currentIndex) % 3 == 0 && currentIndex != 0) {
        result = "," + result;
    }
    return addComma(str, currentIndex - 1, result);
}

var n = '123456';
var output = addComma(n);  
console.log(output);    //輸出123,456

//帶小數(shù)的處理示例
var n = '123456.78';
var output = addComma(n.split('.')[0]) + '.' + n.split('.')[1];
console.log(output);   // 輸出 123,456.78
小葫蘆

The one upstairs is better

function get(num) {
    num = num.split('').reverse().join('')
    return num.match(/\d{1,3}/g).join(',').split('').reverse().join('')
}
阿神
function addComma(str) {
    if(str.length <= 3) {
        return str;
    }
    var res = str.split("");
    var resStr = res.splice(0, 3);
    return resStr.join("") + "," + addComma(res.join(""));
}
伊謝爾倫
'2342342323'.match(/\d{1,3}/g).join(',') // -> 234,234,232,3

The shortest code is here, let’s see if there is any shorter one.

Ty80

From left to right

function myFunc(str) {
    return str.split('').reverse().join('').replace(/\B(?=(?:\d{3})+\b)/g, ',').split('').reverse().join('')
}

From right to left

function myFunc(str) {
    return str.replace(/\B(?=(?:\d{3})+\b)/g, ',')
}
我想大聲告訴你
(234234+"").replace(/\B(?=(\d{3})+(?!\d))/g, ",")

Someone asked this question a few days ago /q/10...
However, the above method is not suitable for decimal points. Since js does not support reverse pre-checking, use another set of regular rules for decimal points.

(234234.23132+"").replace(/\B(?=(\d{3})+(?=\.))/g, ",")

And what the hell is recursion? You don’t need to use recursion for this, right?

PHPzhong
    comdify : function (n){
      var re=/\d{1,3}(?=(\d{3})+$)/g;
      var n1 = String(n).replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,")+s2;});
      return n1;
    }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template