要让income和cost保留小数点两位,可以使用toFixed(2)方法将其转换为字符串并保留两位小数。修改代码如下:

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);

toFixed(2) 方法将数字转换为字符串,并保留小数点后两位。例如:

const number = 123.456789;
const formattedNumber = number.toFixed(2); // 结果为 '123.46'

这样,income和cost就会始终保留小数点两位,以确保计算结果的准确性。


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

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