I have a number n
which is 16 digits long (1111111111111111
). When I add another 1
to this number (making it 11111111111111111
, 17 digits long) it is displayed as 11111111111111112
.
This is understandable because it is larger than Number.MAX_SAFE_INTEGER
, however if I add a 0
instead (making it 11111111111111110
, still 17 digits long), the number is displayed correctly as 11111111111111110
.
let a = 11111111111111111;
let b = 11111111111111110;
console.log(a, b); // 11111111111111112 11111111111111110
The behaviour above occurs with any final digit, apart from 0
. What is the reason for this difference in behaviour?
I have looked at "Large numbers erroneously rounded in Javascript", but that doesn't explain why it is rounded when a non-zero number is added, and not when a zero is added.