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
Release: 2016-05-16 16:59:43
Original
1555 people have browsed it

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;

}

Related labels:
js
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template