范围错误:x 无法转换为 BigInt,因为它不是整数

当对非整数数字使用 BigInt() 函数时,会发生 JavaScript 异常 "x 无法转换为 BigInt,因为它不是整数"。

¥The JavaScript exception "x can't be converted to BigInt because it isn't an integer" occurs when the BigInt() function is used on a number that isn't an integer.

信息

¥Message

RangeError: The number 1.5 cannot be converted to a BigInt because it is not an integer (V8-based & Firefox)
RangeError: Not an integer (Safari)

错误类型

¥Error type

RangeError

什么地方出了错?

¥What went wrong?

使用 BigInt() 函数将数字转换为 BigInt 时,该数字必须是整数(以便 Number.isInteger 返回 true)。

¥When using the BigInt() function to convert a number to a BigInt, the number must be an integer (such that Number.isInteger returns true).

示例

¥Examples

无效案例

¥Invalid cases

js
const a = BigInt(1.5);
// RangeError: The number 1.5 cannot be converted to a BigInt because it is not an integer
const b = BigInt(NaN);
// RangeError: NaN cannot be converted to a BigInt because it is not an integer

有效案例

¥Valid cases

js
const a = BigInt(1);

也可以看看