JavaScript内置对象BigInt实例分析

发布时间:2022-07-15 13:53:21 作者:iii
来源:亿速云 阅读:110

本文小编为大家详细介绍“JavaScript内置对象BigInt实例分析”,内容详细,步骤清晰,细节处理妥当,希望这篇“JavaScript内置对象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 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实例分析”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. JavaScript中内置对象是什么
  2. JavaScript常用内置对象是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javascript bigint

上一篇:SpringBoot监听器模式实例分析

下一篇:vue如何实现多级菜单效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》