此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Math.ceil()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

Math.ceil() 静态方法总是向上舍入,并返回大于等于给定数字的最小整数。

尝试一下

console.log(Math.ceil(0.95));
// Expected output: 1

console.log(Math.ceil(4));
// Expected output: 4

console.log(Math.ceil(7.004));
// Expected output: 8

console.log(Math.ceil(-7.004));
// Expected output: -7

语法

js
Math.ceil(x)

参数

x

一个数值。

返回值

大于等于 x 的最小整数。它的值与 -Math.floor(-x) 相同。

描述

因为 ceil()Math 的静态方法,所以你应始终使用 Math.ceil(),而不是作为你创建的 Math 对象的方法(Math 不是构造函数)。

示例

使用 Math.ceil()

js
Math.ceil(-Infinity); // -Infinity
Math.ceil(-7.004); // -7
Math.ceil(-4); // -4
Math.ceil(-0.95); // -0
Math.ceil(-0); // -0
Math.ceil(0); // 0
Math.ceil(0.95); // 1
Math.ceil(4); // 4
Math.ceil(7.004); // 8
Math.ceil(Infinity); // Infinity

规范

Specification
ECMAScript® 2026 Language Specification
# sec-math.ceil

浏览器兼容性

参见