本文摘自PHP中文网,作者藏色散人,侵删。
js取整数的方法:1、通过“Math.trunc()”方法去除数字的小数部分,保留整数部分;2、通过“Math.round()”方法返回一个数字四舍五入后的整数部分;3、通过“Math.ceil()”方法实现向上取整等等。

本文操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。
JavaScript进行数值取整:
一、Math.trunc()
1、定义
Math.trunc()方法去除数字的小数部分,保留整数部分。
2、语法
3、示例
1 2 3 4 5 6 7 | console.log(Math.trunc(2.01));
console.log(Math.trunc(2.9));
console.log(Math.trunc( '0.22' ));
console.log(Math.trunc(-1.22));
console.log(Math.trunc(-1.56));
console.log(Math.trunc(true));
console.log(Math.trunc(undefined));
|
二、Math.round()
1.定义
Math.round()方法返回一个数字四舍五入后的整数部分。
2、语法
3、示例
1 2 3 4 5 6 7 | console.log(Math. round (2.01));
console.log(Math. round (2.9));
console.log(Math. round ( '0.22' ));
console.log(Math. round (-1.22));
console.log(Math. round (-1.56));
console.log(Math. round (true));
console.log(Math. round (undefined));
|
三、Math.ceil()
阅读剩余部分
相关阅读 >>
如何利用js对css样式进行更改
怎么获取json的key
js (javascript)加密算法库 crypto-js 简介
js实现加载时锁定html页面元素的方法
三种js数组去重的简洁方案
js控制输入框只允许输入小数
js代表什么
js面向对象编程
javascript 和 dart 的区别
jquery怎么写ajax
更多相关阅读请进入《js》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » js怎么取整数