Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  152] [ 7]  / answers: 1 / hits: 19980  / 9 Years ago, sun, march 29, 2015, 12:00:00

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.


More From » string

 Answers
16

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.


[#67260] Friday, March 27, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harleyterryp

Total Points: 290
Total Questions: 92
Total Answers: 95

Location: Montenegro
Member since Sun, May 7, 2023
1 Year ago
;