方法一(通过正则控制): html:
<el-input v-model="inputTable.amount" @input="formatNum(form.amount, 'amount')"
formatNum(val, key) { let temp = val.toString(); temp = temp.replace(/。/g, "."); temp = temp.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符 temp = temp.replace(/^./g, ""); //验证第一个字符是数字 temp = temp.replace(/.{2,}/g, ""); //只保留第一个, 清除多余的 temp = temp.replace(".", "#").replace(/./g, "").replace("#", "."); temp = temp.replace(/^(-)(\d+).(\d\d).13"); //只能输入两个小数 this.form[key] = temp; },
方法二(使用组件): 这个是我最近才发现的,方便多了TT 设置精度precision,即可四舍五入; 再改改样式,隐藏按钮,靠左对齐,最后效果和普通的input无异