JavaScript引入BigInt类型以解决Number类型在处理超过2^53-1的大整数时精度丢失的问题,通过n后缀或BigInt()构造函数创建,支持常规算术与位运算,但不可与Number直接混合运算,需显式转换。BigInt除法向下取整,不支持JSON序列化,建议使用字符串转换处理;推荐在处理大整数ID、加密计算等场景使用,避免精度丢失,提升代码可靠性。

JavaScript引入了原生的
BigInt
Number
2^53-1
-(2^53-1)
使用
BigInt
Number
你可以通过在整数后面添加
n
BigInt
123n
BigInt()
Number
BigInt
BigInt(123)
BigInt("9007199254740991")
const bigNumberLiteral = 9007199254740991n; // 直接使用n后缀 const bigNumberFromNumber = BigInt(9007199254740991); // 从Number转换 const bigNumberFromString = BigInt("900719925474099123456789"); // 从字符串转换,推荐用于超大数字 console.log(bigNumberLiteral); // 9007199254740991n console.log(bigNumberFromNumber); // 9007199254740991n console.log(bigNumberFromString); // 900719925474099123456789n
BigInt
+
-
*
/
%
**
==
===
<
>
BigInt
Number
1n + 1
TypeError
const a = 10n; const b = 3n; console.log(a + b); // 13n console.log(a - b); // 7n console.log(a * b); // 30n console.log(a / b); // 3n (注意:BigInt的除法会向下取整,没有小数部分) console.log(a % b); // 1n console.log(a ** b); // 1000n // 类型转换示例 const num = 5; // console.log(a + num); // TypeError: Cannot mix BigInt and other types, use explicit conversions console.log(a + BigInt(num)); // 15n console.log(Number(a) + num); // 15 (注意:如果BigInt太大,转换为Number可能导致精度丢失)
BigInt
Number
5n / 2n
2n
2.5
BigInt
&
|
^
~
<<
>>
>>>
typeof
BigInt
"bigint"
JavaScript的
Number
-(2^53 - 1)
2^53 - 1
-9007199254740991
9007199254740991
Number
我记得以前在处理一些后端返回的ID,或者像区块链、加密货币这类场景的数值时,经常会遇到这种问题。一个看起来很长的数字,一到前端用
Number
const maxSafeInteger = Number.MAX_SAFE_INTEGER; // 9007199254740991 console.log(maxSafeInteger + 1); // 9007199254740992 console.log(maxSafeInteger + 2); // 9007199254740992 (这里已经开始丢失精度了!)
BigInt
BigInt
高效使用
BigInt
Number
最关键的一点,也是我个人觉得最容易犯错的地方,就是
BigInt
Number
1n + 1
2n
2
2
1n
2n
1
Number
// 错误示范:TypeError // const result = 5n + 2; // 正确示范:显式转换 const result1 = 5n + BigInt(2); // 7n const result2 = Number(5n) + 2; // 7 (注意:这里如果5n的值超出了Number的安全范围,就会丢失精度)
在实际应用中,如果你的数据源(比如从后端API获取的JSON)中包含超出
Number
BigInt(string)
BigInt
Number
const largeIdString = "123456789012345678901234567890n"; // 假设这是从API得到的字符串 // const wrongId = Number(largeIdString); // 这会出错或者精度丢失 const correctId = BigInt(largeIdString.slice(0, -1)); // 如果字符串带'n',需要处理一下 // 或者如果后端直接返回纯数字字符串,更直接 const pureNumberString = "123456789012345678901234567890"; const correctId2 = BigInt(pureNumberString);
尽管
BigInt
陷阱1:JSON序列化问题
BigInt
JSON.stringify()
TypeError: Do not know how to serialize a BigInt
BigInt
const data = {
id: 12345678901234567890n,
name: "Test"
};
// JSON.stringify(data); // TypeError解决方案: 通常的做法是提供一个自定义的
replacer
JSON.stringify
BigInt
Number
const data = {
id: 12345678901234567890n,
name: "Test"
};
const jsonString = JSON.stringify(data, (key, value) =>
typeof value === 'bigint' ? value.toString() : value
);
console.log(jsonString); // {"id":"12345678901234567890","name":"Test"}在接收端,你可能需要一个
reviver
BigInt
陷阱2:除法结果的截断
前面也提到了,
BigInt
console.log(7n / 2n); // 3n // 如果需要浮点数结果 console.log(Number(7n) / Number(2n)); // 3.5
陷阱3:宽松相等(==)与严格相等(===)
BigInt
Number
1n == 1
true
1n === 1
false
===
最佳实践:
n
BigInt
Number
BigInt
Number
Number
BigInt
Number
BigInt
BigInt
BigInt
总的来说,
BigInt
以上就是JS如何实现BigInt?大整数的运算的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号