input金额失焦自动补零和千分位格式化

    技术2022-07-15  60

    在input的@blur事件执行该方法

    blurmoneyformat(inputValue){ //若输入框内容为空则不做处理 if(inputValue == '' || inputValue.length == 0){ return } //判断是否有小数点 if(inputValue.indexOf(".") != -1){ let str = inputValue.split(".") if(str[0].length > 0){ let arr = str[0].split('') let res = arr.every((item,index) => { return item == 0 || item == '0' }) if(res){ if(str[1]){ inputValue = '0.' + str[1] }else{ inputValue = '0.00' } }else{ if(str[1]){ inputValue = str[0].replace(/\b(0+)/g,"") + '.' + str[1] }else{ inputValue = str[0].replace(/\b(0+)/g,"") + '.00' } } } if(str[1].length == 1){ inputValue = inputValue + '0' } }else{ let arr = inputValue.split('') let res = arr.every((item,index) => { return item == 0 || item == '0' }) if(res){ inputValue = '0.00' }else{ inputValue = inputValue.replace(/\b(0+)/g,"") + '.00' } } //千分位格式化 let intNum = inputValue.substring(0, inputValue.indexOf('.')).replace(/\B(?=(?:\d{3})+$)/g, ','); let dot = inputValue.substring(inputValue.indexOf('.'), inputValue.length); inputValue = intNum + dot return inputValue },
    Processed: 0.012, SQL: 9