Home >Web Front-end >JS Tutorial >JS converts numbers into three-digit comma-separated styles (sample code)_javascript skills

JS converts numbers into three-digit comma-separated styles (sample code)_javascript skills

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

Copy code The code is as follows:

function formatNum(num)
{
if(!/^( |-)?(d )(.d )?$/.test(num)){alert("wrong!"); return num;}
var a = RegExp.$1, b = RegExp.$2, c = RegExp.$3;
var re = new RegExp().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