// //保留三位小数 或两位小数
export const forMatMoney=(num,type)=> {
var result = parseFloat(num);
if (isNaN(result)) {
console.log('传递参数错误,请检查!');
return false;
}
result = Math.round(num * 100) / 100;
var s_x = result.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += '.';
}
if(type == 'capacity') {
while (s_x.length <= pos_decimal + 3) {
s_x += '0';
}
}else {
while (s_x.length <= pos_decimal + 2) {
s_x += '0';
}
}
return s_x;
}
// //保留三位小数 或两位小数
export const forMatMoney = (num, type) => {
let count = (type == 'capacity' ? 3 : 2)
if (Number(num) > 0){
return Number(num).toFixed(count)
}else{
return (0).toFixed(count)
}
}
string是没有toFixed()这种方法的
vue 过滤器注册到vue实例里面
import * as filters from '@/common/filters' //filters里面可以写各种各样的方法
Object.keys(filters).forEach(key => {
Vue.filter(key, filters[key])
})
//转换款项类型
changeText(key) {
var Settlementarr={
0: "首款",
1: "尾款",
2: "质保金"
}
return Settlementarr[key]
},
//添加千位符 ,并保留两位小数
keepTwoDecimalFull(num) {
if (typeof(num) != 'number') return num
if (num == 0) return 0.00
// if (/[^0-9\.]/.test(s)) {
// return "invalid value";
// }
console.log(num, "num")
let s = (num).toString()
s = s.replace(/^(\d*)$/, "$1.");
s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
s = s.replace(".", ",");
var re = /(\d)(\d{3},)/;
while (re.test(s)) {
s = s.replace(re, "$1,$2");
}
s = s.replace(/,(\d\d)$/, ".$1");
return s
},