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

View in English Always switch to English

Math.ceil()

基线 广泛可用

自 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

规范

规范
ECMAScript® 2027 Language Specification
# sec-math.ceil

浏览器兼容性

参见