Math.round(),Math.ceil(),Math.floor()的區別

1.Math.round():根據「round」的字面意思「附近、周圍」,能夠猜想該函數是求一個附近的整數,看下面幾個例子就明白。函數

小數點後第一位<5
正數:Math.round(11.46)=11
負數:Math.round(-11.46)=-11
 
小數點後第一位>5
正數:Math.round(11.68)=12
負數:Math.round(-11.68)=-12
 
小數點後第一位=5
正數:Math.round(11.5)=12
負數:Math.round(-11.5)=-11
總結:(小數點後第一位)大於五所有加,等於五正數加,小於五全不加。
 
2.Math.ceil():根據「ceil」的字面意思「天花板」去理解;
例如:
Math.ceil(11.46)=Math.ceil(11.68)=Math.ceil(11.5)=12
Math.ceil(-11.46)=Math.ceil(-11.68)=Math.ceil(-11.5)=-11
 
3.Math.floor():根據「floor」的字面意思「地板」去理解;
例如:
Math.floor(11.46)=Math.floor(11.68)=Math.floor(11.5)=11
Math.floor(-11.46)=Math.floor(-11.68)=Math.floor(-11.5)=-12
相關文章
相關標籤/搜索