欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript?內置對象?BigInt詳細解析

 更新時間:2022年07月15日 10:02:37   作者:??桃子蘸醬?  
這篇文章主要介紹了JavaScript?內置對象?BigInt詳細解析,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下

前言

說起JavaScript中的內置對象,其實又很多,今天我們介紹的是BigInt,在開發(fā)過程中,其實很少使用這個對象,所以你也不知道這個對象。它提供了一種方法來表示大于 2^53 - 1 的整數(shù)。這原本是 Javascript 中可以用 Number表示的最大數(shù)字。BigInt可以表示任意大的整數(shù)。

比較

它在某些方面類似于 Number但也有不同點:

  • 不能用于 Math對象中的方法
  • 不能和任何 Number實例混合運算,兩者必須轉換成同一種類型
  • 在兩種類型轉換時,可能會丟失精度

創(chuàng)建

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 靜態(tài)方法將 BigInt 值轉換為一個 -2^(width-1) 與 2^(width-1)-1 之間的有符號整數(shù)。

const max = 2n ** (64n - 1n) - 1n;

BigInt.asIntN(64, max);
// ? 9223372036854775807n

BigInt.asIntN(64, max + 1n);
// ? -9223372036854775808n
// negative because of overflow

asUintN()

BigInt.asUintN 靜態(tài)方法將 BigInt 轉換為一個 0 和 2^width-1 之間的無符號整數(shù)。

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"

它可以本地化數(shù)字格式,這樣一來也不用你專門為此功能寫API進行轉換。為此,請確保使用 locales 參數(shù)指定該語言

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內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論