Home  >  Article  >  Web Front-end  >  JS thousandth algorithm implementation ideas_javascript skills

JS thousandth algorithm implementation ideas_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:27:361228browse
Copy code The code is as follows:

function commafy() {
var num = document.getElementById( "NumA").value;
//1. First remove the spaces and determine whether it is a null value or a non-number
num = num "";
num = num.replace(/[ ]/g, " ");
if (num == "") {
alert("Null value, end");
return;
}
if (isNaN(num)) {
alert("Not a number, end");
return;
}
//2. Depending on whether there is a decimal point, handle it according to the situation
var index = num.indexOf(".");
if (index==-1) {//No decimal point
var reg = /(-?d )(d{3})/;
while (reg.test(num)) {
num = num.replace(reg, "$1,$2");
}
} else {
var intPart = num.substring(0, index);
var pointPart = num. substring(index 1, num.length);
var reg = /(-?d )(d{3})/;
while (reg.test(intPart)) {
intPart = intPart. replace(reg, "$1,$2");
}
num = intPart "." pointPart;
}
return alert(num);
}
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