'Javascript convert bigInt to string
I am trying to convert the following big int to a string in javascript with no success. My goal would end up with '582235852866076672'
var foo = 582235852866076672;
console.log(foo); // 582235852866076700
var baz = "'" + 582235852866076672 + "'";
console.log(baz); // '582235852866076700'
var emptyString = 582235852866076672+'';
console.log(emptyString); // 582235852866076700
var n = foo.toString();
console.log(n); // 582235852866076700
I figured the number was too big and was loosing precision as a result. I included the bigint library with no success:
var bigint = require('bigint');
var bigintLibrary = bigint(582235852866076672).toString();
console.log(bigintLibrary); //582235852866076700
The method toSting in the bigint library states:
"Print out the bigint instance in the requested base as a string."
I appreciate all help and comments. Thanks.
Solution 1:[1]
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());
Solution 2:[2]
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.
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Top-Master |
| Solution 2 | C. Tewalt |
| Solution 3 |
