16

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.

srm
  • 655
  • 1
  • 8
  • 21
  • Where is the number coming from? The number you're talking about can't exist as a normal javascript number, so the source is important. – Aaron Dufour Mar 29 '15 at 20:41
  • @AaronDufour the number is coming in the params of a post request, I have tried var jsonString = JSON.stringify(582235852866076672); with the same result console.log(jsonString); // 582235852866076700. – srm Mar 29 '15 at 21:05
  • You're going to have to manually parse the params, then. Unless you can have the client pass it as a string instead of a number? The precision is lost as soon as the library parses it into a number, so by the time it gets to your code it's too late. – Aaron Dufour Mar 29 '15 at 21:56

4 Answers4

10

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.

Jeff Hubbard
  • 9,822
  • 3
  • 30
  • 28
  • Thanks for the reply, but how do I get the integer 582235852866076672 into string format? Everything I try results in 582235852866076700 – srm Mar 29 '15 at 20:16
  • 1
    You put quotes around it, like I did. You can't represent a number bigger than the max size in JavaScript directly--it gets truncated (as you're seeing). – Jeff Hubbard Mar 29 '15 at 21:07
  • 1
    Thanks I am seeing that, I was more curious how to convert the integer 582235852866076672 to string the '582235852866076672', the data is coming in as an integer and I have to convert it to a string, I cannot just add quotes I have to convert it – srm Mar 29 '15 at 23:35
  • So actually this seems to be a precision issue--as in, JavaScript is unable to represent that number precisely. The number is greater than 2^53, which is the max number representable in JavaScript. – Jeff Hubbard Mar 30 '15 at 00:29
  • It seems like I will have to manipulate the data into a string before it reaches the javascript side. – srm Mar 30 '15 at 14:42
  • Yep. Sorry to have such a crappy answer, but that's what you're stuck with. I'll update my answer accordingly. – Jeff Hubbard Mar 30 '15 at 22:40
  • 1
    To clarify, `2^53` is the maximum _integer_ representable in JavaScript. The maximum _number_ representable in JavaScript is `2^1024`. – Aaron Dufour Apr 02 '15 at 05:11
  • Not according to http://www.ecma-international.org/ecma-262/5.1/#sec-8.5, which specifies that the Number type has exactly 2^53 values--which maps directly to IEEE 754 with an exception for NaN values (which all map to a single NaN value) and positive/negative Infinity. Unless I'm horribly misreading the spec. – Jeff Hubbard Apr 03 '15 at 19:23
10

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

Top-Master
  • 7,611
  • 5
  • 39
  • 71
3

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.

C. Tewalt
  • 2,271
  • 2
  • 30
  • 49
John Daly
  • 31
  • 1
2

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

Jacob Hornbeck
  • 398
  • 2
  • 19
  • 2
    Your examples are actually incorrect. `BigInt(1234567890987654321234567890)` gives `1234567890987654297979715584n`, and `BigInt(135445264365246564)` gives `135445264365246560n` (note `...60n` at the end). And that's working as intended, because the Number you're using as input can't hold that much precision. Using the `BigInt(123)` notation is generally not recommended because of this scalability issue; prefer `123n`. – jmrk May 14 '22 at 22:48
  • Thank you for letting me know! When I tested the examples I gave, I guess I didn't see that they did not work (or didn't see that the output wasn't the same as the input)... I will edit my answer to include what you said. – Jacob Hornbeck May 15 '22 at 02:11