首页  >  文章  >  web前端  >  js 数值项目的格式化函数代码_javascript技巧

js 数值项目的格式化函数代码_javascript技巧

WBOY
WBOY原创
2016-05-16 18:27:12742浏览
复制代码 代码如下:

//--------------------------------------------
// 删除千分点。

//--------------------------------------------
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;
}
}
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn