本文小编为大家详细介绍“javascript内置对象BigInt实例分析”,内容详细,步骤清晰,细节处理妥当,希望这篇“JavaScript内置对象BigInt实例分析”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧
本文小编为大家详细介绍“javascript内置对象BigInt实例分析”,内容详细,步骤清晰,细节处理妥当,希望这篇“JavaScript内置对象BigInt实例分析”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
它在某些方面类似于 Number
,但也有不同点:
不能用于 Math
对象中的方法
不能和任何 Number
实例混合运算,两者必须转换成同一种类型
在两种类型转换时,可能会丢失精度
const theBiggestInt = 9007199254740991n;const alsoHuge = BigInt(9007199254740991);// ↪ 9007199254740991nconst hugeString = BigInt("9007199254740991");// ↪ 9007199254740991nconst hugeHex = BigInt("0x1fffffffffffff");// ↪ 9007199254740991nconst hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");// ↪ 9007199254740991n
BigInt.asIntN 静态方法将 BigInt 值转换为一个 -2^(width-1) 与 2^(width-1)-1 之间的有符号整数。
const max = 2n ** (64n - 1n) - 1n;BigInt.asIntN(64, max);// ↪ 9223372036854775807nBigInt.asIntN(64, max + 1n);// ↪ -9223372036854775808n// negative because of overflow
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
返回一个字符串,该字符串具有此 BigInt 的 language-sensitive 表达形式。
const bigint = 123456789123456789n;// German uses period for thousandsconsole.log(bigint.toLocaleString('de-DE'));// expected output: "123.456.789.123.456.789"
它可以本地化数字格式,这样一来也不用你专门为此功能写api进行转换。为此,请确保使用 locales 参数指定该语言
var bigint = 123456789123456789n;// German uses period for thousandsconsole.log(bigint.toLocaleString('de-DE'));// → 123.456.789.123.456.789// Arabic in most Arabic speaking countries uses Eastern Arabic digitsconsole.log(bigint.toLocaleString('ar-EG'));// → ١٢٣٬٤٥٦٬٧٨٩٬١٢٣٬٤٥٦٬٧٨٩// India uses thousands/lakh/crore separatorsconsole.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 decimalconsole.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 Indonesianconsole.log(bigint.toLocaleString(['ban', 'id']));// → 123.456.789.123.456.789
返回一个字符串,表示指定
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"
返回 BigInt 对象包装的原始值。
console.log(typeof Object(1n));// expected output: "object"console.log(typeof Object(1n).valueOf());// expected output: "bigint"
读到这里,这篇“JavaScript内置对象BigInt实例分析”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网精选频道。
--结束END--
本文标题: JavaScript内置对象BigInt实例分析
本文链接: https://www.lsjlt.com/news/343633.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0