0

I am getting a json response back with my data but when I am trying to use one of the values (number) the response is converting to scientific notation. I never encountered something like this so I am just going to post what is happening.

When I use console.log(response) the value looks like that doctorid":2015031216033174968087 and when I am trying to use the doctorid the value becomes 2.015031216033175e+21. This happens even if I output the value in the console by using console.log(response.doctorid). Any idea what might be causing such a behavior?

Geo
  • 3,160
  • 6
  • 41
  • 82

1 Answers1

4

Unlike python and ruby, Javascript doesn't support arbitrary precision integers. The maximal numeric value that can be exactly represented in JS is 9007199254740991. Bigger numbers have an approximate representation and are converted to strings using scientific notation.

The only way to avoid that in your case is to encode IDs as strings in JSON:

"doctorid": "2015031216033174968087"
georg
  • 211,518
  • 52
  • 313
  • 390
  • 1
    "Numbers greater than that are converted to floats" --- that's incorrect (at least it must be rephrased). **EVERY** number in JS is an IEEE 754 double. – zerkms Mar 12 '15 at 22:16
  • That's probably going to fix my issue. I will try it and accept your answer if it works. Thanks – Geo Mar 12 '15 at 22:18
  • 1
    "can only be displayed as floats" --- that's ugly as well. Representation has nothing to do with the data behind it. – zerkms Mar 12 '15 at 22:19
  • 1
    @zerkms: well, I did my best ;) If you have a better idea how to express what I meant here, feel free to edit. – georg Mar 12 '15 at 22:22
  • I would just remove that complete phrase. " The maximal integer value JS can work with is 9007199254740991" states all one needs to know. If they need more - they go to ES and IEEE 754 specification :-) – zerkms Mar 12 '15 at 22:23
  • 1
    @zerkms: turned CW, feel free to edit. – georg Mar 12 '15 at 22:30