本文详细介绍 JavaScript 中的 BigInt 内置对象,用于表示超过 Number 最大安全整数(2^53 - 1)的大整数。内容涵盖 BigInt 的创建方式、与 Number 的区别、类型转换时的精度丢失风险,以及 asIntN、asUintN、toLocaleString、toString 和 valueOf 等静态与实例方法的实际用法。
前言
JavaScript 提供了多种内置对象,其中 BigInt 相对较少被日常开发直接使用,因此可能不为所有开发者所熟知。BigInt 提供了一种机制来精确表示大于 2^53 - 1 的整数,这一界限原本是 JavaScript 中 Number 类型所能安全表示的最大数值。通过 BigInt,开发者可以处理任意大小的整数运算。
比较
BigInt 在行为上部分类似于 Number,但存在关键差异:
- 它无法直接调用
Math对象中的数学方法 - BigInt 不能与任何
Number实例进行混合运算,两者必须转换为相同的类型才能操作 - 在两种类型之间进行转换时,可能会发生精度丢失
创建
const theBiggestInt = 9007199254740991n;
const alsoHuge = BigInt(9007199254740991);
// ↪ 9007199254740991n
const hugeString = BigInt("9007199254740991");
// ↪ 9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff");
// ↪ 9007199254740991n
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");
// ↪ 9007199254740991n方法
asIntN()
BigInt.asIntN 是一个静态方法,它将 BigInt 值转换为指定宽度内的有符号整数,范围介于 -2^(width-1) 到 2^(width-1)-1 之间。
const max = 2n ** (64n - 1n) - 1n;
BigInt.asIntN(64, max);
// ↪ 9223372036854775807n
BigInt.asIntN(64, max + 1n);
// ↪ -9223372036854775808n
// negative because of overflowasUintN()
BigInt.asUintN 是一个静态方法,它将 BigInt 转换为指定宽度内的无符号整数,范围介于 0 到 2^width-1 之间。
const max = 2n ** 64n - 1n;
function check64bit(number) {
(number > max) ?
console.log('Number doesn\'t fit in unsigned 64-bit integer!') :
console.log(BigInt.asUintN(64, number));
}
check64bit(2n ** 64n);
// expected output: "Number doesn't fit in unsigned 64-bit integer!"
check64bit(2n ** 32n);
// expected output: 4294967296ntoLocaleString()
该方法返回一个字符串,包含此 BigInt 基于语言环境的格式化表达。
const bigint = 123456789123456789n;
// German uses period for thousands
console.log(bigint.toLocaleString('de-DE'));
// expected output: "123.456.789.123.456.789"它支持本地化数字格式,无需额外编写 API 即可实现转换。使用时需通过 locales 参数指定目标语言。
var bigint = 123456789123456789n;
// German uses period for thousands
console.log(bigint.toLocaleString('de-DE'));
// → 123.456.789.123.456.789
// Arabic in most Arabic speaking countries uses Eastern Arabic digits
console.log(bigint.toLocaleString('ar-EG'));
// → ١٢٣٬٤٥٦٬٧٨٩٬١٢٣٬٤٥٦٬٧٨٩
// India uses thousands/lakh/crore separators
console.log(bigint.toLocaleString('en-IN'));
// → 1,23,45,67,89,12,34,56,789
// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(bigint.toLocaleString('zh-Hans-CN-u-nu-hanidec'));
// → 一二三,四五六,七八九,一二三,四五六,七八九
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(bigint.toLocaleString(['ban', 'id']));
// → 123.456.789.123.456.789toString()
返回表示指定
BigInt对象的字符串。注意,结果字符串末尾的 "n" 并非字符串内容的一部分。
console.log(1024n.toString());
// expected output: "1024"
console.log(1024n.toString(2));
// expected output: "10000000000"
console.log(1024n.toString(16));
// expected output: "400"valueOf()
返回 BigInt 对象所包装的原始 BigInt 值。
console.log(typeof Object(1n));
// expected output: "object"
console.log(typeof Object(1n).valueOf());
// expected output: "bigint"到此这篇关于JavaScript 内置对象 BigInt详细解析的文章就介绍到这了,更多相关JavaScript BigInt内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!