js数字转千分位 保留小数位
var vm = new Vue({
filters: {
thousandth2: function (value) {
if (!value) return '0.00';
var intPart = Number(value) - Number(value) % 1; //获取整数部分
var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); //将整数部分逢三一断
var floatPart = ".00"; //预定义小数部分
var value2Array = value.toString().split(".");
//=2表示数据有小数位
if (value2Array.length == 2) {
floatPart = value2Array[1].toString(); //拿到小数部分
floatPart = parseFloat("0." + floatPart).toFixed(2);
var value3Array = floatPart.split(".")
floatPart = value3Array[1].toString();
if (value3Array[0].toString() == 1) {
intPart = intPart + 1;
intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
return intPartFormat + "." + floatPart;
}
if (floatPart.length == 1) { //补0,实际上用不着
return intPartFormat + "." + floatPart;
} else {
return intPartFormat + "." + floatPart;
}
} else {
return intPartFormat + floatPart;
}
},
}
})