小程序中小数输入框保留固定小数位数的做法
2020-12-07 本文已影响0人
townYouth
在common.js中
// value小数点后保留places位小数
const decimal = (value, places =2) => {
if (/^(\d?)+(\.\d{0,places})?$/.test(value)) {//正则验证,小数点后不能大于places位数字
return value
} else {
return value.substring(0, value.indexOf('.') +Number(places) +1)
}
}

在index.js中
import { decimal }from '../../utils/common'
inputQuantities (e) {
let value =e.detail.value
this.data.jigongInfo.quantities = decimal(value,2)
this.setData({
jigongInfo:this.data.jigongInfo
})
}
