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