这段代码包含三个函数:caltaxExemptIncome()calculateCost()setInputValue()

  • caltaxExemptIncome() 函数用于计算税收免除收入。
  • calculateCost() 函数用于计算成本,该函数根据收入金额判断成本,若收入小于等于4000,则成本为800,否则成本为收入的20%。
  • setInputValue() 函数用于设置输入值,该函数将传入的值设置到this.form 对象的incomecosttaxExemptIncome 属性中,并使用 parseFloat() 函数将其转换为浮点数,使用 toFixed(2) 函数将其保留两位小数。

代码中使用 toFixed(2) 函数来确保所有计算结果都保留两位小数。

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);
  }
},
setInputValue(value) {
  this.form.income = value ? parseFloat(value).toFixed(2) : null;
  this.form.cost = value ? parseFloat(value).toFixed(2) : null;
  this.form.taxExemptIncome = value ? parseFloat(value).toFixed(2) : null;
}

本示例展示了使用JavaScript函数保留两位小数点的计算方法,适用于需要精确计算并显示两位小数结果的场景。


原文地址: http://www.cveoy.top/t/topic/plgd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录