init
This commit is contained in:
29
uni_modules/nutui-uni/components/input/util.ts
Normal file
29
uni_modules/nutui-uni/components/input/util.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
function trimExtraChar(value: string, char: string, regExp: RegExp) {
|
||||
const index = value.indexOf(char)
|
||||
|
||||
if (index === -1)
|
||||
return value
|
||||
|
||||
if (char === '-' && index !== 0)
|
||||
return value.slice(0, index)
|
||||
|
||||
return value.slice(0, index + 1) + value.slice(index).replace(regExp, '')
|
||||
}
|
||||
|
||||
export function formatNumber(value: string, allowDot = true, allowMinus = true) {
|
||||
if (allowDot)
|
||||
value = trimExtraChar(value, '.', /\./g)
|
||||
|
||||
else
|
||||
value = value.split('.')[0]
|
||||
|
||||
if (allowMinus)
|
||||
value = trimExtraChar(value, '-', /-/g)
|
||||
|
||||
else
|
||||
value = value.replace(/-/, '')
|
||||
|
||||
const regExp = allowDot ? /[^-0-9.]/g : /[^-0-9]/g
|
||||
|
||||
return value.replace(regExp, '')
|
||||
}
|
||||
Reference in New Issue
Block a user