在 JavaScript 中,数字类型是非常重要的数据类型之一。然而,在传统的 JavaScript 中,数字类型只能表示 2 的 53 次方以内的整数,而不能表示更大的整数。为了解决这个问题,ES12 引入了 BigInt 数据类型,可以表示任意大的整数。
BigInt 的定义和使用
在 JavaScript 中,BigInt 是一个全局对象,用来表示任意大的整数。可以使用 BigInt()
函数来创建一个 BigInt 类型的值,例如:
const bigIntNum = BigInt(12345678901234567890); console.log(bigIntNum); // 12345678901234567890n
注意到,在数字后面加上 n
后缀,就可以创建一个 BigInt 类型的值。如果没有这个后缀,那么就会被解析为普通的 Number 类型。
BigInt 类型的值支持所有的算术运算符,例如加、减、乘、除、取模等。
const a = BigInt(12345678901234567890); const b = BigInt(98765432109876543210); console.log(a + b); // 111111111111111111100n console.log(b - a); // 86419753208641975320n console.log(a * b); // 12193263113702179524844702789589098700n console.log(b / a); // 799999999.99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999n console.log(b % a); // 8888888888888888880n
BigInt 和 Number 的转换
由于 BigInt 和 Number 是两种不同的数据类型,它们之间需要进行转换。可以使用 BigInt()
函数将 Number 类型的值转换为 BigInt 类型,也可以使用 Number()
函数将 BigInt 类型的值转换为 Number 类型。
const num = 12345678901234567890; const bigIntNum = BigInt(num); console.log(typeof bigIntNum); // bigint const bigIntNum2 = 98765432109876543210n; const num2 = Number(bigIntNum2); console.log(typeof num2); // number
需要注意的是,在将一个超出 Number 范围的 BigInt 类型值转换为 Number 类型时,可能会出现精度丢失的情况。
const bigIntNum = BigInt(9007199254740992); // 2^53 const num = Number(bigIntNum); console.log(num); // 9007199254740992,精度未丢失 const bigIntNum2 = BigInt(9007199254740993); // 2^53 + 1 const num2 = Number(bigIntNum2); console.log(num2); // 9007199254740992,精度丢失
BigInt 和字符串的转换
与 Number 类型不同,BigInt 类型的值可以表示任意大的整数,因此在将 BigInt 类型的值转换为字符串时,需要注意到字符串的长度可能会非常大。
可以使用 toString()
函数将 BigInt 类型的值转换为字符串,也可以使用 BigInt()
函数将字符串转换为 BigInt 类型的值。
const bigIntNum = BigInt(12345678901234567890); const str = bigIntNum.toString(); console.log(str); // "12345678901234567890" const str2 = "98765432109876543210"; const bigIntNum2 = BigInt(str2); console.log(bigIntNum2); // 98765432109876543210n
示例代码
下面是一个使用 BigInt 计算斐波那契数列的示例代码:
// javascriptcn.com 代码示例 function fibonacci(n) { let a = BigInt(0); let b = BigInt(1); for (let i = 0; i < n; i++) { let c = a + b; a = b; b = c; } return a; } console.log(fibonacci(100)); // 3736710778780434371n
总结
ES12 中引入了 BigInt 数据类型,可以表示任意大的整数。BigInt 类型的值支持所有的算术运算符,与 Number 类型可以互相转换。在处理大整数计算时,可以使用 BigInt 类型来避免精度丢失的问题。
来源:JavaScript中文网 ,转载请注明来源 本文地址:https://www.javascriptcn.com/post/655b1e49d2f5e1655d54a543