方法一:
使用x | 0
,快速实现去除小数位保留整数。
复制代码
1
2
3console.log(99.55 | 0); // 99 console.log(33.55 | 0); // 33
方法二:
使用.tofixed(0)
,保留小数位,注意此方法会将数字四舍五入。
复制代码
1
2
3
4var number = 99.55; console.log(number.tofixed(0)); // 100 console.log(number.tofixed(1)); // 99.6
方法三:
使用Math.floor()
向下取整。
复制代码
1
2
3Math.floor(99.55); // 99 Math.floor(-99.55); // -100
最后
以上就是积极帅哥最近收集整理的关于js 快速移除小数点后数字保留整数的全部内容,更多相关js内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复