npm install number-precision --save
import NP from 'number-precision'//浮点数处理依赖
/**
*
NP.round(0.105, 2); 求近似值= 0.11, not 0.1
NP.strip(0.09999999999999998); // =0.1
NP.plus(2.3, 2.4); // =4.7 +
NP.minus(1.0, 0.9); // =0.1 -
NP.times(3, 0.3); // =0.9 *
NP.divide(0.9, 0.3); // =3 ÷
*/
应用示例
//当有小数转4位 无则去除小数点
numFormat(num){
let point = num.lastIndexOf(".");
let numLen = num.length;
let hz = num.substring(point, numLen);
if(hz > 0){
return num
}else{
return num.substring(0,point)
}
},
// 随输入框值变化其他数值 结果全部以4位小数记
changeInput(e, row, colname) {
//修改 单价 和 税率 数量
if(colname == 'price' || colname == 'itemtaxrate' || colname == "quantity" ){
console.log('compute');
// 未税金额 = 单价*数量
//row.amount = row.price * row.quantity;
//console.log(NP);踩坑 依赖有更新方法名变了 打印下方法名
row.amount =this.numFormat(NP.times(row.price,row.quantity).toFixed(4))
//含税单价 = 未税单价* (1+税率)
//row.taxprice = row.price*(1 + row.itemtaxrate*0.01);
row.taxprice =this.numFormat(NP.times(row.price,(1 + row.itemtaxrate*0.01)).toFixed(4))
//含税金额 = 未税金额 *(1+税率)
//row.taxamount = row.price*row.quantity*(1 + row.itemtaxrate*0.01);
row.taxamount =this.numFormat(NP.times(row.price,row.quantity*(1 + row.itemtaxrate*0.01)).toFixed(4))
}
//修改 含税单价
if( colname == 'taxprice' ){
//未税单价 = 含税单价 / 1 + 税率
//row.price = row.taxprice / (1 + row.itemtaxrate*0.01 )
row.price =this.numFormat(NP.divide(row.taxprice,(1 + row.itemtaxrate*0.01 )).toFixed(4))
//未税金额 = 未税单价 * 数量
// row.amount = row.price * row.quantity
row.amount = this.numFormat(NP.times(row.price, row.quantity).toFixed(4))
//含税金额 = 含税单价 * 数量
//row.taxamount = row.price*row.quantity*(1 + row.itemtaxrate*0.01)
row.taxamount = this.numFormat(NP.times(NP.divide(row.taxprice,(1 + row.itemtaxrate*0.01 )),row.quantity*(1 + row.itemtaxrate*0.01)).toFixed(4))
}
//修改未税金额
if( colname == 'amount' ){
//未税单价 = 含税单价 / 1 + 税率
row.price =this.numFormat(NP.divide(row.amount,row.quantity).toFixed(4))
//含税单价
row.taxprice =this.numFormat(NP.divide(row.price,(1 + row.itemtaxrate*0.01 )).toFixed(4))
//含税金额 = 未税金额 * 1+ 税
row.taxamount = this.numFormat(NP.times(row.amount,(1 + row.itemtaxrate*0.01 )).toFixed(4))
}
},