Number.isInteger()

Number.isInteger() 静态方法判断传递的值是否为整数。

¥The Number.isInteger() static method determines whether the passed value is an integer.

Try it

语法

¥Syntax

js
Number.isInteger(value)

参数

¥Parameters

value

要测试的值是否为整数。

返回值

¥Return value

如果给定值是整数,则为布尔值 true。否则 false

¥The boolean value true if the given value is an integer. Otherwise false.

描述

¥Description

如果目标值为整数,则返回 true,否则返回 false。如果值为 NaNInfinity,则返回 false。对于可以表示为整数的浮点数,该方法还将返回 true。如果该值不是数字,它将始终返回 false

¥If the target value is an integer, return true, otherwise return false. If the value is NaN or Infinity, return false. The method will also return true for floating point numbers that can be represented as integer. It will always return false if the value is not a number.

请注意,某些数字文字虽然看起来像非整数,但实际上表示整数 - 由于 ECMAScript 浮点数编码 (IEEE-754) 的精度限制。例如,5.00000000000000015 仅相差 1e-16,这太小而无法表示。(作为参考,Number.EPSILON 存储 1 和下一个大于 1 的可表示浮点数之间的距离,大约为 2.22e-16。)因此,5.0000000000000001 将使用与 5 相同的编码来表示,从而使 Number.isInteger(5.0000000000000001) 返回 true

¥Note that some number literals, while looking like non-integers, actually represent integers — due to the precision limit of ECMAScript floating-point number encoding (IEEE-754). For example, 5.0000000000000001 only differs from 5 by 1e-16, which is too small to be represented. (For reference, Number.EPSILON stores the distance between 1 and the next representable floating-point number greater than 1, and that is about 2.22e-16.) Therefore, 5.0000000000000001 will be represented with the same encoding as 5, thus making Number.isInteger(5.0000000000000001) return true.

类似地,大约 Number.MAX_SAFE_INTEGER 大小的数字将遭受精度损失,并使 Number.isInteger 返回 true,即使它不是整数。(实际阈值根据需要多少位来表示小数而变化 - 例如,Number.isInteger(4500000000000000.1)true,但 Number.isInteger(4500000000000000.5)false。)

¥In a similar sense, numbers around the magnitude of Number.MAX_SAFE_INTEGER will suffer from loss of precision and make Number.isInteger return true even when it's not an integer. (The actual threshold varies based on how many bits are needed to represent the decimal — for example, Number.isInteger(4500000000000000.1) is true, but Number.isInteger(4500000000000000.5) is false.)

示例

¥Examples

使用 isInteger

¥Using isInteger

js
Number.isInteger(0); // true
Number.isInteger(1); // true
Number.isInteger(-100000); // true
Number.isInteger(99999999999999999999999); // true

Number.isInteger(0.1); // false
Number.isInteger(Math.PI); // false

Number.isInteger(NaN); // false
Number.isInteger(Infinity); // false
Number.isInteger(-Infinity); // false
Number.isInteger("10"); // false
Number.isInteger(true); // false
Number.isInteger(false); // false
Number.isInteger([1]); // false

Number.isInteger(5.0); // true
Number.isInteger(5.000000000000001); // false
Number.isInteger(5.0000000000000001); // true, because of loss of precision
Number.isInteger(4500000000000000.1); // true, because of loss of precision

规范

Specification
ECMAScript Language Specification
# sec-number.isinteger

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看