Tried this in node v0.8.15 and firebug:
(123123123123123123123).toString(10)
Result:
'123123123123123130000'
Anyone know what's going on?
Tried this in node v0.8.15 and firebug:
(123123123123123123123).toString(10)
Result:
'123123123123123130000'
Anyone know what's going on?
JavaScript stores numbers as 64-bit floating-point IEEE 754 values, so the maximum precision is 2^53 (9007199254740992). Anything beyond that, and the least-significant digits are just set to 0.
For example:
(9007199254740992).toString() // 9007199254740992
(90071992547409925).toString() // 90071992547409920
(900719925474099255).toString() // 900719925474099200
// etc
If you really need that many digits of precision, you should look into a library such as bignum (the author helpfully lists other such libraries).
All numbers in javascript (unlike many other languages) are stored as (AFAIK 64-bit) floating-point numbers (never integers).
If you're not familiar with how this works, it is like scientific notation (AKA standard form): a * 10b
When you type 123123123123123123123, it is too big to fit in the a
part of the float. The value of a
will be stored as 1.2312312312312313, truncating the lowest-value overflowing digits, and b
is set to 20 to provide an approximation as good as possible of the original value. When you get the string value, 1.2312312312312313 * 1020 is calculated, giving 123123123123123130000.