Javascript convert bigInt to string
You can use the BigInt
methods BigInt.prototype.toLocaleString()
, or BigInt.prototype.toString()
. Hope that helps.
Click here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt for more BigInt
info.
As of 2019, you can use the built-in BigInt and BigInt literal, first, do something like:
// Cast to BigInt:
var result = BigInt("1234567801234567890");
result = BigInt(1234567801234567890);
// Or use BigInt literal directly (with n suffix).
result = 1234567801234567890n
Then use built-in toString
method:
console.log('my BigInt is', result.toString());
See documentation on MDN
This is a precision issue--the number you're getting back (582235852866076672
) is bigger than the max number representable in JavaScript, which is 2^53 or 9007199254740992
.
It seems like even though there are three answers, only one of them kind of actually answers the question, but it's not the accepted answer...
This is my answer:
First of all, you don't need to import the BigInt library... BigInt is built-in to JavaScript (at least it is now), you can access it by calling the function: BigInt(...)
or by adding an 'n' at the end of a number: 45625625443n
BigInt can hold very big numbers, last I checked, the limit is 1 billion bits 1. That means you could hold an integer around 1×10109, which in most cases you most likely won't need any integer that big.
As I showed above, you can build a BigInt by using the constructor (BigInt(...)
) or by adding an 'n' to the end (45625625443n
) 2
When using the constructor, the parameter can be any of the following (but as jmrk said in the comments, when putting in a number into the constructor, it will still lose precision, so don't use a number):
// String
BigInt("1234567890987654321234567890")
// -> 1234567890987654321234567890n
// Number
BigInt(1234567890987654321234567890)
// -> 1234567890987654297979715584n
/* As said above, using a number in the constructor may lose precision,
as seen here just input a string or use the n */
// BigInt
BigInt(1234567890987654321234567890n)
// -> 1234567890987654321234567890n
// Boolean
BigInt(false) // 0n
BigInt(true) // 1n
You can't mix types in operations. You can't do any of the normal operations with normal JavaScript numbers, you must convert them into BigInts first.
let big_int_number = BigInt(135445264365246564)
// Incorrect
big_int_number + 1365
// Correct (either one)
big_int_number + 1365n
big_int_number + BigInt(1365)
And finally to answer the real question: To turn a BigInt into a string, you just need to use the built-in methods 3:
let big_int_number = BigInt("135445264365246564")
// or
let big_int_number = 135445264365246564n
// Using BigInt.prototype.toString()
let string_version = big_int_number.toString()
/* You probably don't need this method,
but I just put it here just because */
// Using BigInt.prototype.toLocaleString()
let string_version = big_int_number.toLocaleString()
This works for both constructions ways...
let n_constructed = 1234567890987654321234567890n
n_constructed.toString() // 1234567890987654321234567890n
n_constructed.toLocaleString() // 1234567890987654321234567890n
let constructered = BigInt("1234567890987654321234567890")
constructered.toString() // 1234567890987654321234567890n
constructered.toLocaleString() // 1234567890987654321234567890n
If you have any questions about BigInt, visit MDN's reference page on BigInt