*
* @param {*} value 输入的值
* @param {*} digitCount 要限制输入的小数位数
* @returns 格式化之后的数字
*/
export const formatPositiveNumber = (value, digitCount=2) => {
if(!Number(value)) return value;
if(Number(value) && digitCount ===0){
return parseInt(value);
}
const reg = new RegExp(`^(\\d+\\.?\\d{0,${digitCount}}).*$`);
value = String(value).replace(/[^\d.]/g, "");
value = value.replace(reg, "$1");
return value;
};
import { formatPositiveNumber } from '@/utils/inputFormat.js';
<template>
<el-input
v-model="number1"
clearable
size="small"
maxlength="5"
@input="$event=formatPositiveNumber($event, 2)"
placeholder="请输入">
</el-input>
</template>