JavaScript 内置对象 BigInt详细解析

lxf2023-10-25 23:20:01
摘要

这篇文章主要介绍了JavaScript 内置对象 BigInt详细解析,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下

目录
  • 前言
  • 比较
  • 创建
  • 方法
    • asIntN()
    • asUintN()
    • toLocaleString()
    • toString()
    • valueOf()

前言

说起javascript中的内置对象,其实又很多,今天我们介绍的是BigInt,在开发过程中,其实很少使用这个对象,所以你也不知道这个对象。它提供了一种方法来表示大于 2^53 - 1 的整数。这原本是 Javascript 中可以用 Number表示的最大数字。BigInt可以表示任意大的整数。

比较

它在某些方面类似于 Number但也有不同点:

  • 不能用于 Math对象中的方法
  • 不能和任何 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 overflow

asUintN()

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: 4294967296n

toLocaleString()

返回一个字符串,该字符串具有此 BigInt 的 language-sensitive 表达形式。

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.789

toString()

返回一个字符串,表示指定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 对象包装的原始值。

console.log(typeof Object(1n));
// expected output: "object"

console.log(typeof Object(1n).valueOf());
// expected output: "bigint"

到此这篇关于JavaScript 内置对象 BigInt详细解析的文章就介绍到这了,更多相关JavaScript BigInt内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.adminjs.cn!