复制代码 代码如下:
function commafy() {
var num = document.getElementById("NumA").value;
//1.先去除空格,判断是否空值和非数
num = num + "";
num = num.replace(/[ ]/g, "");
if (num == "") {
alert("空值,结束");
return;
}
if (isNaN(num)) {
alert("非数,结束");
return;
}
//2.针对是否有小数点,分情况处理
var index = num.indexOf(".");
if (index==-1) {//无小数点
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);
}
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!