先判断输入的数是否为空,如果是空,则跳出函数,如果非空再进行parseFloat()函数进行解析浮点数。
format() {
const inputValue = this.form.priceGun; // 获取输入值
// 检查输入值是否为空或只包含空格
if (!inputValue.trim()) {
this.form.priceGun = ""; // 清空输入框的值
return; // 退出函数
}
const parsedValue = parseFloat(inputValue); // 将输入值转换为浮点数
// 检查转换后的值是否为NaN
if (isNaN(parsedValue)) {
this.form.priceGun = ""; // 清空输入框的值
return; // 退出函数
}
const formattedValue = parsedValue.toFixed(2); // 将浮点数保留两位小数
this.form.priceGun = formattedValue; // 更新输入框的值
// // 将输入值转为浮点数,并补充到小数点后两位
// const value = parseFloat(this.form.priceGun).toFixed(2);
// // 更新输入框的值
// this.form.priceGun = value;
},