Home  >  Article  >  Web Front-end  >  js numerical value converted to 3-digit comma separated sample code_javascript skills

js numerical value converted to 3-digit comma separated sample code_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:59:411506browse

Example code:

Copy code The code is as follows:

function formatNum(strNum) {

if (strNum.length <= 3) {

return strNum;

}

if (!/^( |-)?(d )(.d )?$/.test(strNum)) {

return strNum;

}

var a = RegExp.$1, b = RegExp.$2, c = RegExp.$3;

var re = new RegExp();

re.compile("(\d)(\d{3})(,|$)");

while (re.test(b)) {

b = b.replace(re, "$1,$2$3");

}

return a "" b "" c;

}

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