Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
Math.floor() 靜態方法會將給定的數字無條件捨去,並回傳小於或等於該數字的最大整數。
一個數字。
小於或等於 x 的最大整數。其值與 -Math.ceil(-x) 相同。
因為 floor() 是 Math 的靜態方法,所以你應該總是使用 Math.floor(),而不是在你建立的 Math 物件上呼叫方法(Math 不是一個建構子)。
在此範例中,我們實作了一個名為 decimalAdjust() 的方法,它是 Math.floor()、Math.ceil() 和 Math.round() 的增強方法。雖然這三個 Math 函式總是將輸入調整到個位數,但 decimalAdjust 接受一個 exp 參數,該參數指定了數字應調整到小數點左側的位數。例如,-1 表示它將在小數點後保留一位數字(如同「× 10-1」)。此外,它還允許你透過 type 參數選擇調整方式——round、floor 或 ceil。
它透過將數字乘以 10 的次方,然後將結果四捨五入到最接近的整數,再除以 10 的次方來實現。為了更好地保持精度,它利用了 Number 的 toString() 方法,該方法以科學記號(如 6.02e23)表示大數或小數。
| ECMAScript® 2027 Language Specification # sec-math.floor |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年8月26日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.