SoFunction
Updated on 2025-03-03

Several ways to add thousands of characters to format numbers

Comma-separated processing method for every three digits

Regular way:

Copy the codeThe code is as follows:

"15000000".split("").reverse().join("").replace(/(\d{3})/g, "$1,").split("").reverse().join("");

"115000000".split("").reverse().join("").replace(/(\d{3})(?=[^$])/g, "$1,").split("").reverse().join("");

var str = '123123211312.333123'.replace(/(?=(?!^)(?:\d{3})+(?:\.|$))(\d{3}(\.\d+$)?)/g,',$1');
(str)

'1500000000000'.replace(/\B(?=(\d{3})+$)/g,',')

Circular search method

Copy the codeThe code is as follows:

function formatNumber(value) {
    value = ();
    if ( <= 3) {
        return value;
    } else {
        return formatNumber((0, - 3)) + ',' + ( - 3);
    }
}

toLocaleString function:

Copy the codeThe code is as follows:

15000000..toLocaleString();