1

I am calling a RESTFUL web services which returns JSON data. The response contains a numeric value(without quotes) 10000000000223169, but after getting the JavaScript object against this response I get 10000000000223168.

The JSON response shown directly in the browser also shows 10000000000223168 but the FireBug HTTP response in content tab shows the correct value i.e. 10000000000223169

Jaffy
  • 575
  • 1
  • 6
  • 15
  • Is your service returning a sequential number ? If so, maybe your code is calling the RESTFUL service two times (showing the first). It's odd that your getting a result exactly 1 number less! – Zachary Apr 12 '13 at 13:29
  • 1
    @Zachary no, it's nothing to do with that. It's because there's no such (legal) integer in JS. – Alnitak Apr 12 '13 at 13:30

2 Answers2

4

That's because numbers in JavaScript are IEEE754 double precision floats.

Integers can only be all represented up to 2^53.

Your number is too big to be sent as number, you should send it as string.

If you need to do computation with this number in your browser, the easiest solution would be to use a big number dedicated library (see this related question).

Community
  • 1
  • 1
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Many big numbers can be represented exactly, you have to be lucky. Of course, the bigger your number, the less probable it is that it can be represented exactly. – Denys Séguret Apr 12 '13 at 13:30
2

10000000000223169 is beyond the range that may be represented as an exact integer in JavaScript (and hence JSON). It's roughly 2 ^ 53.151 and IEEE 754 only permits 53 bits of resolution in the mantissa of a double precision floating point number.

To transport it without modification it would need to be sent as a string.

In the client that's using this code, it'll also need to be stored either as a string, or as a 64-bit integer (assuming you're using a language with such a data type).

If you try to store it in an IEEE 754 float (which is the default numeric type in JS it'll still get rounded to 53 bits of precision as soon as you attempt to perform any mathematical operation on it.

Alnitak
  • 334,560
  • 70
  • 407
  • 495