Home  >  Article  >  Web Front-end  >  Several ways to add thousand characters to formatted numbers in node.js_node.js

Several ways to add thousand characters to formatted numbers in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 15:51:331289browse

Separate every three characters with commas

Regular method:

Copy code The 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');
console.log(str)

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

Loop search method

Copy code The code is as follows:

function formatNumber(value) {
value = value.toString();
If (value.length <= 3) {
         return value;
} else {
            return formatNumber(value.substr(0, value.length - 3)) ',' value.substr(value.length - 3);
}
}

toLocaleString function:

Copy code The code is as follows:

15000000..toLocaleString();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn