8

I have worked the same process in JavaScript with number datatype

var num = 9223372036854775807;

But when I try print this variable in browser

alert(num)

the value changed to 9223372036854776000

Any ideas?

Sachin
  • 20,754
  • 28
  • 93
  • 162
developerone
  • 131
  • 1
  • 3

4 Answers4

15

Javascript numbers are actually double precision floats. The largest integer that can be precisely stored is 253, much less then your 263-1.

alert( Math.pow(2, 53) - 1 ) // 9007199254740991
alert( Math.pow(2, 53)     ) // 9007199254740992
alert( Math.pow(2, 53) + 1 ) // 9007199254740992
hugomg
  • 66,048
  • 22
  • 153
  • 240
1

Javascript stores longs as javascript numbers (64 bit floats). And 9223372036854776000 is MAX.

Do you do numeric operations or can you store it as a string?

Unmitigated
  • 46,070
  • 7
  • 44
  • 60
Eystein Bye
  • 4,766
  • 2
  • 19
  • 18
1

Because the maximum integer value of a number in js is 9007199254740992. See What is JavaScript's highest integer value that a Number can go to without losing precision? for more

Community
  • 1
  • 1
Geuis
  • 39,646
  • 54
  • 150
  • 215
0

The problem is exactly as described by @missingno, probably the best solution, if you really need to handle big numbers is the BigNumber Class: http://jsfromhell.com/classes/bignumber

It is worth noting that this is not going to be an efficient way to do calculations compared with other languages, but it is probably the easiest to manage in JavaScript.

Billy Moon
  • 54,763
  • 23
  • 128
  • 231