JavaScipt中的Math.ceil() 、Math.floor() 、Math.round() 三個函數(shù)的理解
首先還是看看JavaScript: The Definitive Guide, 4th Edition中對三個函數(shù)的定義。
Math.ceil(): round a number up
Arguments: Any numeric value or expression
Returns: The closest integer greater than or equal to x.
-----------------------------------------------------------------------------------------------
Math.floor(): round a number down
Arguments: Any numeric value or expression
Returns: The closest integer less than or equal to x.
-----------------------------------------------------------------------------------------------
Math.round(): round to the nearest integer
Arguments: Any number.
Returns: The integer closest to x.
以前一直會三個函數(shù)的使用產(chǎn)生混淆,現(xiàn)在通過對三個函數(shù)的原型定義的理解,其實很容易記住三個函數(shù)。
現(xiàn)在做一個總結(jié):
1. Math.ceil()用作向上取整。
2. Math.floor()用作向下取整。
3. Math.round() 我們數(shù)學(xué)中常用到的四舍五入取整。
相關(guān)文章
微信公眾號開發(fā) 自定義菜單跳轉(zhuǎn)頁面并獲取用戶信息實例詳解
這篇文章主要介紹了微信公眾號開發(fā) 自定義菜單跳轉(zhuǎn)頁面并獲取用戶信息實例詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12深入理解JavaScript系列(36):設(shè)計模式之中介者模式詳解
這篇文章主要介紹了深入理解JavaScript系列(36):設(shè)計模式之中介者模式詳解,中介者模式(Mediator)是指用一個中介對象來封裝一系列的對象交互,需要的朋友可以參考下2015-03-03JavaScript中的toLocaleLowerCase()方法使用詳解
這篇文章主要介紹了JavaScript中的toLocaleLowerCase()方法使用詳解,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06javaScript中兩個等于號和三個等于號之間的區(qū)別介紹
這篇文章主要介紹了javaScript中兩個等于號和三個等于號之間的區(qū)別,還不知道的朋友可以學(xué)習(xí)下2014-06-06