2

I perform an AJAX call to generate an ID. This ID is sent back to the client in the response and shown in an input field. I was made aware that the ID displayed in the browser is not the one generated - the last digit differs. On the serverside I serialize data to pass it back to the client using Adobe ColdFusion's own serializeJSON() function. It recognizes the sequence of digits and serializes it as a number. I logged the contents of my variables on different places in my codde, it looked fine all the way. Only the browser does not do what I want/expect.

I boiled it down to this simple sample:

var stru = {"MYID":2761602017000540006};
console.dir(stru);

The console logs 2761602017000540000 instead of 2761602017000540006

Why is that? Is this number too large to be stored in JavaScript?

Bernhard Döbler
  • 1,960
  • 2
  • 25
  • 39

3 Answers3

4

Is the number too large to be stored in JavaScript?

Yes, the max safe integer is 9,007,199,254,740,991 and the number you're attempting to send is 2,761,602,017,000,540,006 (which is a factor of ~1000x larger).

This is because the JavaScript number type follows the IEEE 754 64-bit floating point number format, which doesn't allow as for as large of numbers as a 64-bit integer normally would. You can see the definition of the number type value here in the ECMAScript spec 4.3.20.

I suggest you send the ID over as a String.

Patrick Barr
  • 1,123
  • 7
  • 17
3

In JavaScript, one has at most 53 bits for integers. so you can not put integers larger that 53bits into javascript variables, so the other way is to use strings for saving this long id . I hope that this help you

Arash Khajelou
  • 560
  • 3
  • 16
3

As Arash said, your number is too long (more than 53 bits). You can have more information on this topic: Javascript long integer The only solution seems to be using string instead of numbers