JavaScript 计算免税收入并保留两位小数
以下是完整的解决方案:
caltaxExemptIncome() {
const income = parseFloat(this.form.income ? this.form.income.toString().replace(',', '') : 0).toFixed(2);
const cost = parseFloat(this.form.cost ? this.form.cost.toString().replace(',', '') : 0).toFixed(2);
const taxExemptIncome = ((income - cost) * 0.3).toFixed(2);
if (income === 0 || income === null) {
this.form.cost = 0;
this.form.taxExemptIncome = 0;
} else {
this.form.cost = this.calculateCost(income);
this.form.taxExemptIncome = parseFloat(taxExemptIncome);
}
},
calculateCost(income) {
if (income <= 4000) {
return 800;
} else {
return (income * 0.2).toFixed(2);
}
}
在计算taxExemptIncome时,使用toFixed(2)方法将结果保留两位小数。
在判断income是否为空时,使用||运算符替代&&运算符,同时将条件中的income改为0,使得条件成立。
在calculateCost函数中,使用toFixed(2)方法将结果保留两位小数。
原文地址: http://www.cveoy.top/t/topic/plfz 著作权归作者所有。请勿转载和采集!