5
<?php  
$int = 1968401665333658496;  
echo json_encode( array("$int",$int) );  
?>  

Recieved in browser: [ "1968401665333658496" , 1968401665333658600 ]

It "rounds off" my integer?

Btw : PHP_INT_MAX = 9223372036854775807 ~ PHP Version 5.3.2-1ubuntu4.7
No problems with these huge integers anywhere (PHP, MySQL or Javascript)
- until json_encode() screws it up (silently btw..)

eldarerathis
  • 35,455
  • 10
  • 90
  • 93
T4NK3R
  • 4,245
  • 3
  • 23
  • 25

3 Answers3

4

It's not just a JSON issue. If you put

alert(1968401665333658496);

in firebug console you get 1968401665333658600

You're probably hitting the JS max value.

There's a discussion on that here : What is JavaScript's highest integer value that a Number can go to without losing precision?

Community
  • 1
  • 1
JohnP
  • 49,507
  • 13
  • 108
  • 140
2

Javascript has no concept of integers, according to the standard all numbers are IEEE doubles, which means they have 52 bits of mantissa. this leads to a practical maximum "integer" value of 2^53 before any loss of precision.

I am not sure how you didn't have problems with numbers this large in JS alone - if you didn't your JS implementation is not standards compliant.

tobyodavies
  • 27,347
  • 5
  • 42
  • 57
  • Wow, that's sobering. Having worked professionally with JS, not knowing such a basic fact. Still 2^53 = 9007199254740992, still leaves me plenty of wriggleroom for my (8 types of) unique-key scheme : ) THANK YOU! – T4NK3R Apr 04 '11 at 17:28
1

Quote your integer value and handle the conversion on the client.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176