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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM