DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js 數值項目的格式化函數代碼
js 數值項目的格式化函數代碼
編輯:關於JavaScript     
復制代碼 代碼如下:
//--------------------------------------------
// 刪除千分點。

//--------------------------------------------
function removeComma(number) {
var num = number.replace(new RegExp(",","g"),"");
if(/^[-+]?[0-9]+(\.[0-9]+)?$/.test(num)) {
return num;
} else {
return number;
}
}

//--------------------------------------------
//添加千分點。
//--------------------------------------------
function addKannma(number) {

var num = number.replace(new RegExp(",","g"),"");

// 正負號處理
var symble = "";
if(/^([-+]).*$/.test(num)) {
symble = num.replace(/^([-+]).*$/,"$1");
num = num.replace(/^([-+])(.*)$/,"$2");
}

if(/^[0-9]+(\.[0-9]+)?$/.test(num)) {
var num = num.replace(new RegExp("^[0]+","g"),"");
if(/^\./.test(num)) {
num = "0" + num;
}

var decimal = num.replace(/^[0-9]+(\.[0-9]+)?$/,"$1");
var integer= num.replace(/^([0-9]+)(\.[0-9]+)?$/,"$1");

var re=/(\d+)(\d{3})/

while(re.test(integer)){
integer =integer.replace(re,"$1,$2")
}
return symble + integer + decimal;

} else {
return number;
}
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved