Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert strings to bigInt in JavaScript

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

Example

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

Resultant value not matching since JavaScript only allows integers up to (2^53)-1.

Is there any way to overcome this?

like image 235
rselvaganesh Avatar asked Apr 24 '16 17:04

rselvaganesh


People also ask

How do you represent BigInt in JavaScript?

BigInt is a built-in object in JavaScript that provides a way to represent whole numbers larger than 253-1. The largest number that JavaScript can reliably represent with the Number primitive is 253-1, which is represented by the MAX_SAFE_INTEGER constant.

How do you convert BigInt to string?

math. BigInteger. toString() method returns the decimal String representation of this BigInteger. This method is useful to convert BigInteger to String.

How do I convert my number to BigInt?

Syntax. To differentiate a BigInt from a Number you have to append the letter n to the end of the number. For example, 156 is a Number, 156n is a BigInt.

How do you declare BigInt?

A BigInt value, also sometimes just called a BigInt, is a bigint primitive, created by appending n to the end of an integer literal, or by calling the BigInt() function (without the new operator) and giving it an integer value or string value.


3 Answers

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.

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

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:

const value = 78099864177253771992779766288266836166272662n;
like image 175
Mathias Bynens Avatar answered Oct 14 '22 04:10

Mathias Bynens


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

like image 37
kamlesh pandey Avatar answered Oct 14 '22 02:10

kamlesh pandey


You can use 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
like image 35
Shanoor Avatar answered Oct 14 '22 03:10

Shanoor