当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Javascript BigInt.asIntN()用法及代码示例


BigInt.asIntN()方法是JavaScript中的内置方法,用于将BigInt值包装为-2之间的有符号整数width-1和2width-1-1。

用法:

BigInt.asIntN(width, bigint);;

参数:该函数接受上述和以下所述的两个参数:



  • width:此参数保存可用于整数大小的位数。
  • bigint:此参数保存要钳位以适合提供的位的整数。

返回值:此方法返回bigint模2的值width作为有符号整数。

以下示例说明了JavaScript中的BigInt.asIntN()方法:

范例1:

let maxlimit = 2n ** (64n - 1n) - 1n; 
  
function GFG(num) { 
  (num > maxlimit) ? 
    console.log("Number exceed the limit of"+ 
                  " signed 64-bit integer!"):
    console.log(BigInt.asIntN(64, num)); 
} 
  
GFG(2n ** 16n); 
GFG(2n ** 32n); 
GFG(2n ** 64n);      

输出:

65536n
4294967296n
"Number exceed the limit of signed 64-bit integer!"

范例2:

const max = 2n ** (64n - 1n) - 1n; 
  
console.log(BigInt.asIntN(64, max)); 
console.log(BigInt.asIntN(64, max + 1n)); 
console.log(BigInt.asIntN(32, max));

输出:

9223372036854775807n
-9223372036854775808n
-1n

支持的浏览器:下面列出了BigInt.asIntN()方法支持的浏览器:

  • 谷歌浏览器
  • Firefox
  • Opera
  • Safari
  • Edge



相关用法


注:本文由纯净天空筛选整理自SHUBHAMSINGH10大神的英文原创作品 JavaScript | BigInt.asIntN() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。