JavaScript Math类的三个ceil()、floor()、round()的区别

来源:互联网 发布:2016年网络关键词 编辑:程序博客网 时间:2024/05/17 02:29

这三个都是将小数变成整数的函数,但是每一个都有不同的作用。

ceil():将小数部分一律向整数部分进位。

程序代码

Math.ceil(12.2)//返回13
Math.ceil(12.7)//返回13
Math.ceil(12.0)//返回12
 
floor():一律舍去,仅保留整数。
 程序代码
Math.floor(12.2)//返回12
Math.floor(12.7)//返回12
Math.floor(12.0)//返回12
 
round():进行四舍五入
程序代码
Math.round(12.2)//返回12
Math.round(12.7)//返回13
Math.round(12.0)//返回12
----------------------------------------------------------------------------------
Tags: javascript ceil() floor() round()
----------------------------------------------------------------------------------