DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JS的千分位算法實現思路
JS的千分位算法實現思路
編輯:關於JavaScript     
復制代碼 代碼如下:
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);
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved