js Math对象的round(),ceil(),floor()方法

1、Math.round()

功能:四舍五入取整。

语法:Math.round(x)

参数:

         x:一个浮点数。

返回值:与x最接近的整数。

注:当出现两个最接近的整数,将返回最大的那个整数。

示例:

Math.round(1.3);//1
Math.round(1.5);//2
Math.round(1.7);//2
Math.round(-1.3);//-1
Math.round(-1.5);//-1
Math.round(-1.7);//-2

2、Math.floor()

功能:对一个数进行下取整。。

语法:Math.round(x)

参数:

         x:一个浮点数。

返回值:返回小于或等于x,并且与之最接近的整数。

示例:

Math.floor(1.3);//1
Math.floor(1.5);//1
Math.floor(1.7);//1
Math.floor(-1.3);//-2
Math.floor(-1.5);//-2
Math.floor(-1.7);//-2

2、Math.ceil()

功能:对一个数进行上取整。。

语法:Math.ceil(x)

参数:

         x:一个浮点数。

返回值:返回大于或等于x,并且与之最接近的整数。

示例:

Math.ceil(1.3);//2
Math.ceil(1.5);//2
Math.ceil(1.7);//2
Math.ceil(-1.3);//-1
Math.ceil(-1.5);//-1
Math.ceil(-1.7);//-1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章