I’ve run into this problem before, but with floating point numbers. Similar to this other post. But now it’s with normal numbers.
I was trying to do this:
var
str = 'asdf_10150663120210603',
num = +str.split('_')[1];
console.log(num);
// 10150663120210603 expected
// 10150663120210604 actually resulting
And so I tried, in Node & Chrome Inspector, to simply output the number.
console.log(10150663120210603);
// 10150663120210604 actually resulting
Is there any crazy way, hackish or not, to make a variable equal to 10150663120210603. Or must I use strings?
Floating point numbers in JavaScript are 64-bit IEEE-754 floating point values. They have only 52 bits for the mantissa, and your number requires 54 bits to represent. It’s just being rounded off.
If it’s rounded to 10150663120210604 it can be represented in 52 bits.