在Math中,floor、round、cell分别对应向下取整、四舍五入和向上取整。

方法 描述
floor 对x进行向下取舍。
round 四舍五入取整。
ceil 对数进行向上取舍。

 JavaScript之Math数字取整——floor()、round()、ceil()_取整

 

 

定义和用法
  • floor() 方法返回小于等于x的最大整数。
  • round() 方法可把一个数字舍入为最接近的整数——即四舍五入法。
  • ceil() 方法可对一个数进行上舍入。

round()和ceil()如果传递的值是一个整数,该值不变。

浏览器支持

所有的主要浏览器都支持floor()、round()和ceil()方法。

语法
Math.floor(x)
Math.round(x)
Math.ceil(x)
参数值
参数 描述
x 必需。任意数值或表达式。
返回值
类型 描述
Number 整数。floor、round、ceil方法返回的整数。
技术细节
JavaScript版本 1.0
实例
1 console.log(Math.floor(0.60)); // 返回0
2 console.log(Math.floor(0.40)); // 返回0
3 console.log(Math.round(0.60)); // 返回1
4 console.log(Math.round(0.40)); // 返回0
5 console.log(Math.ceil(0.60)); // 返回1
6 console.log(Math.ceil(0.40)); // 返回1
参考网址

 

有志者,事竟成,破釜沉舟,百二秦关终属楚; 苦心人,天不负,卧薪尝胆,三千越甲可吞吴。