两个数值相减且保留两位小数
export const getMinus = (num1, num2) => {
const sum1 = isNaN(Number(num1)) ? 0 : Number(num1);
const sum2 = isNaN(Number(num2)) ? 0 : Number(num2);
const minus = sum1 - sum2;
return Number(minus.toFixed(2));
};
// 根据两值计算百分比,且保留两位小数,四舍五入
export const getPercent = (num1, num2) => {
if (!num2 || Number(num2) === 0 || isNaN(Number(num2))) return 0;
if (isNaN(Number(num1))) return 0;
const percent = Math.round((Number(num1)) / (Number(num2)) * 10000 )/ 100;
return percent ? percent.toFixed(2) * 1 : percent * 1;
};
//如果是表格里面的某些列的计算则在页面上调getRowData函数,传入scope.row和该列的字段
export const getRowData = (data, type) => {
data.query11 = getPercent(data.query9, data.query8);
data.query12 = getPercent(data.query10, data.query8);
data.query13 = getMinus(data.query12, data.query11);
const obj = {
11: data.query11,
12: data.query12,
13: data.query13,
};
return obj[type] || 0;
};
//某些值需要加%,0也要加%
formatValue(value) {
const unit = this.type == 1 ? '%' : '';
if(!value) return 0 + unit;
if(isNaN(Number(value))) return (0 + unit);
return value + unit;
},