当前第2页 返回上一页
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 12 13 | 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怎么移除属性
javascript使用什么来标识
为什么使用vue的作用域插槽?什么时候使用?
不能编辑javascript的程序有什么
javascript中ie兼容出现的问题及解决方法
html5中的script属性及js同步和异步加载实现代码详解
javascript中数组元素是什么
手把手带你了解javascript中变量和词法环境
javascript的call方法怎么用
详解js中值传递和引用传递的区别
更多相关阅读请进入《javascript》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » javascript怎么判断是否为整数