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

 

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