当前第2页 返回上一页
1 2 3 4 5 6 7 8 9 10 11 | function isInteger(obj) {
return parseInt(obj, 10) === obj
}
isInteger(3)
isInteger(3.3)
isInteger( '' )
isInteger( '3' )
isInteger(true)
isInteger([])
isInteger(1000000000000000000000)
|
原因是parseInt在解析整数之前强迫将第一个参数解析成字符串。这种方法将数字转换成整型不是一个好的选择。
方式四、通过位运算判断
1 2 3 4 5 6 7 8 9 10 11 | function isInteger(obj) {
return (obj | 0) === obj
}
isInteger(3)
isInteger(3.3)
isInteger( '' )
isInteger( '3' )
isInteger(true)
isInteger([])
isInteger(Math.pow(2, 32))
|
方式五、ES6提供了Number.isInteger
1 2 3 4 5 6 | Number.isInteger(3)
Number.isInteger(3.1)
Number.isInteger( '' )
Number.isInteger( '3' )
Number.isInteger(true)
Number.isInteger([])
|
相关免费学习推荐:javascript(视频)
以上就是javascript如何判断是否为整数的详细内容,更多文章请关注木庄网络博客!
返回前面的内容
相关阅读 >>
vue框架是什么
angular2 ngmodel模块的详细介绍
浏览器内核以及浏览器兼容的问题分析
cookie和session的区别总结
javascript是h5吗
javascript中如何获取当前时间
深入了解javascript中的object(对象)
url完整结构以及同源跨域处理的介绍
哪个不属于javascript的特征
定义javascript数组的方法有哪些
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » javascript如何判断是否为整数