JavaScript 内置对象 BigInt

介绍

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

比较

它在某些方面类似于 Number,但也有不同点: - 不能用于 Math对象中的方法 - 不能和任何 Number实例混合运算,两者必须转换成同一种类型 - 在两种类型转换时,可能会丢失精度

创建

```JavaScript 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 之间的有符号整数。 ```JavaScript 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 之间的无符号整数。

```JavaScript 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 表达形式。

```JavaScript const bigint = 123456789123456789n;

// German uses period for thousands console.log(bigint.toLocaleString('de-DE')); // expected output: "123.456.789.123.456.789" ``` 它可以本地化数字格式,这样一来也不用你专门为此功能写API进行转换。为此,请确保使用 locales 参数指定该语言

```JavaScript 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" 不是字符串的一部分

```JavaScript 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 对象包装的原始值。

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

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

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值