如何在JavaScript中将字符串转换为bigint

时间:2021-12-31 16:48:25

I need to convert a string to bigint like BigInteger in Javascript

我需要将字符串转换为bigint,如Javascript中的BigInteger

Example

例子

var reqId = "78099864177253771992779766288266836166272662";
var result = parseInt(reqId);
document.write(result);

Resultant value not matching since JavaScript allows up to 53.

结果值不匹配,因为JavaScript允许最多53。

Is there any way to overcome this?

有没有办法克服这个问题?

5 个解决方案

#1


6  

BigInt is now a native JavaScript language feature. It's at Stage 3 in the TC39 process and it's shipping in V8 v6.7 and Chrome 67.

BigInt现在是一个本地JavaScript语言特性。在TC39过程的第三阶段,它在V8 v6.7和Chrome 67上发布。

To turn a string containing a valid numeric literal into a BigInt, use the global BigInt function:

要将包含有效数值文字的字符串转换为BigInt,请使用全局BigInt函数:

const string = '78099864177253771992779766288266836166272662';
BigInt(string);
// 78099864177253771992779766288266836166272662n

If you just want to hardcode the numeric value into your code, there is no need to convert from a string; use a BigInt literal instead:

如果您只想将数值硬编码到代码中,则不需要从字符串进行转换;使用BigInt文字代替:

const value = 78099864177253771992779766288266836166272662n;

#2


3  

You can use a JavaScript lib called BigInteger.js for the purpose.it is an arbitrary-length integer library for Javascript, allows arithmetic operations on integers of unlimited size, notwithstanding memory and time limitations.This lib can be download from this link.Like var largeNumber = bigInt("75643564363473453456342378564387956906736546456235345"); You can find documentation of lib here https://www.npmjs.com/package/big-integer

您可以使用名为BigInteger的JavaScript库。js为目的。它是一个任意长度的Javascript整数库,允许对无限大小的整数进行算术操作,尽管存在内存和时间限制。这个库可以从这个链接下载。如var largeNumber = bigInt(“75643564363456345634564387956906736546456235345”);您可以在这里找到lib的文档https://www.npmjs.com/package/biginteger

#3


0  

You can use Number or parseFloat. Either way you get scientific numbers:

您可以使用Number或parseFloat。无论哪种方式你都能得到科学的数据:

new Number('78099864177253771992779766288266836166272662')

or

parseFloat('78099864177253771992779766288266836166272662')

Result:

结果:

7.809986417725377e+43

#4


-1  

You can use mathjs:

您可以使用mathjs:

var reqId = "78099864177253771992779766288266836166272662";
var myBigNumber = math.bignumber(reqId);
var res = math.add(myBigNumber, 1);
console.log(myBigNumber.toString());
// 7.8099864177253771992779766288266836166272662e+43
console.log(res.toString());
// 7.8099864177253771992779766288266836166272663e+43

#5


-4  

You can use the constructor to construct a new BigInteger object out of said string. BigInteger Constructor

可以使用构造函数从该字符串构造一个新的BigInteger对象。BigInteger构造函数

#1


6  

BigInt is now a native JavaScript language feature. It's at Stage 3 in the TC39 process and it's shipping in V8 v6.7 and Chrome 67.

BigInt现在是一个本地JavaScript语言特性。在TC39过程的第三阶段,它在V8 v6.7和Chrome 67上发布。

To turn a string containing a valid numeric literal into a BigInt, use the global BigInt function:

要将包含有效数值文字的字符串转换为BigInt,请使用全局BigInt函数:

const string = '78099864177253771992779766288266836166272662';
BigInt(string);
// 78099864177253771992779766288266836166272662n

If you just want to hardcode the numeric value into your code, there is no need to convert from a string; use a BigInt literal instead:

如果您只想将数值硬编码到代码中,则不需要从字符串进行转换;使用BigInt文字代替:

const value = 78099864177253771992779766288266836166272662n;

#2


3  

You can use a JavaScript lib called BigInteger.js for the purpose.it is an arbitrary-length integer library for Javascript, allows arithmetic operations on integers of unlimited size, notwithstanding memory and time limitations.This lib can be download from this link.Like var largeNumber = bigInt("75643564363473453456342378564387956906736546456235345"); You can find documentation of lib here https://www.npmjs.com/package/big-integer

您可以使用名为BigInteger的JavaScript库。js为目的。它是一个任意长度的Javascript整数库,允许对无限大小的整数进行算术操作,尽管存在内存和时间限制。这个库可以从这个链接下载。如var largeNumber = bigInt(“75643564363456345634564387956906736546456235345”);您可以在这里找到lib的文档https://www.npmjs.com/package/biginteger

#3


0  

You can use Number or parseFloat. Either way you get scientific numbers:

您可以使用Number或parseFloat。无论哪种方式你都能得到科学的数据:

new Number('78099864177253771992779766288266836166272662')

or

parseFloat('78099864177253771992779766288266836166272662')

Result:

结果:

7.809986417725377e+43

#4


-1  

You can use mathjs:

您可以使用mathjs:

var reqId = "78099864177253771992779766288266836166272662";
var myBigNumber = math.bignumber(reqId);
var res = math.add(myBigNumber, 1);
console.log(myBigNumber.toString());
// 7.8099864177253771992779766288266836166272662e+43
console.log(res.toString());
// 7.8099864177253771992779766288266836166272663e+43

#5


-4  

You can use the constructor to construct a new BigInteger object out of said string. BigInteger Constructor

可以使用构造函数从该字符串构造一个新的BigInteger对象。BigInteger构造函数