简体   繁体   English

如何在 Javascript 中存储大于 2147483647 的整数?

[英]How can I store an integer number that's greater than 2147483647 in Javascript?

From what I understand integers range from据我所知,整数范围从

 -2147483648 through 2147483647

I am confused because I noticed some references to big int in javascript.我很困惑,因为我注意到 javascript 中一些对 big int 的引用。 Can someone explain how I can store an integer number larger than 2147483647?有人可以解释我如何存储大于 2147483647 的整数吗?

为了超越 JavaScript 的内部数字限制,请使用类似bignum 库的东西(我发现的只是一个随机,研究一个好的库作为练习留给 OP)。

BigInteger.js是一个用于 Javascript 的任意长度整数库,允许对无限大小的整数进行算术运算,尽管有内存和时间限制。

Depending on what you are doing the number, you can handle integers up to +/- (2^53)-1 or 9007199254740991 .根据您所做的数字,您可以处理高达 +/- (2^53)-1 或9007199254740991

While not all browsers support Number.MAX_SAFE_INTEGER yet, you can calculate it with:虽然并非所有浏览器都支持Number.MAX_SAFE_INTEGER但您可以使用以下方法计算它:

Math.pow(2, 53) - 1 

Note: This works when in pure math functions, but many bitwise operations are limited to 16 bit integers, as well as the index in arrays.注意:这在纯数学函数中有效,但许多按位运算仅限于 16 位整数,以及数组中的索引。

More details can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER更多细节可以在这里找到: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

two ways we can store big int我们可以通过两种方式存储 big int

const bigIntVal=123243435454656565657575n;

or

const bigIntVal=BigInt("123243435454656565657575");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 if [on hold] 检测数字是否大于 20 或大于 50 - How can I detect if a number is greater than 20 or greater than 50 with an if [on hold] 如何找到大于 const M 的第一个数字? - How Can I find the first number greater than const M? 如果 window 宽度小于给定值,我如何使用 JavaScript 隐藏 Html 中的文本,并在大于该值时显示它 - How can i hide a text in Html using JavaScript if the window width is lower than a given value and show it if it's greater than the value javascript如果数字大于数字 - javascript if number greater than number 如何检查javaScript中某个数量的数字是否大于另一个数字? - How to check if a number is greater than another number in some quantity in javaScript? javascript测试值是否为数字和大于0的数字 - javascript testing if a value is a number AND a number greater than 0 如何在 ECMAScript 中检查数字是否大于 Number.MAX_SAFE_INTEGER - How to check if number greater than Number.MAX_SAFE_INTEGER in ECMAScript 如何在Javascript中验证给定数字是否为整数? - How can I validate if a given number is integer or not in Javascript? 如何使用javascript将十进制数更改为整数 - how can i change decimal number to integer using javascript 如何在Javascript函数中处理ArrowKeys和&lt;(大于)? 哪个事件和哪个代码(charCode或keyCode)? - How can I handle ArrowKeys and < (Greater Than) in a Javascript function? Which event and which code (charCode Or keyCode)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM