17

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.

3
  • 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. Commented Mar 29, 2015 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. Commented Mar 29, 2015 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. Commented Mar 29, 2015 at 21:56

4 Answers 4

15

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

Sign up to request clarification or add additional context in comments.

Comments

11

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.

8 Comments

Thanks for the reply, but how do I get the integer 582235852866076672 into string format? Everything I try results in 582235852866076700
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).
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
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.
To clarify, 2^53 is the maximum integer representable in JavaScript. The maximum number representable in JavaScript is 2^1024.
|
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.

Comments

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

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.