interceptionStr(str) {/*判断字符串是否有小数,如果有只取后8位*/
if (str === undefined || str === null || isNaN(str) || str === '') {
return 0
}
str = String(str)
let strSplitArr = [], first = null, strSecond = null
if (str.indexOf('.') != -1) { /*是否有小数*/
strSplitArr = str.split('.')
if (strSplitArr[1].length > 8) {
strSecond = strSplitArr[1].slice(0, 8)
first = strSplitArr[0]
strSplitArr = `${first}.${strSecond}`
} else {
strSplitArr = str
}
} else {
strSplitArr = str
}
return Number(strSplitArr)
}
判断字符串是否有小数,如果有只取后几位
最新推荐文章于 2021-07-08 17:54:18 发布
本文介绍了一种用于处理字符串中浮点数的JavaScript函数。该函数能够判断输入字符串是否包含有效的小数部分,并将其标准化为最多保留八位小数的形式。适用于需要精确控制小数精度的场景。
1450

被折叠的 条评论
为什么被折叠?



