I am using express-cassandra
npm package to connect to database, The below code is giving negative value while converting Long
value into Integer
.
var num = 13315766168394088000;
Result is:-
var valueFromInt = models.datatypes.Long.fromInt(num);
=> Long: -1152696320
var valueFromString = models.datatypes.Long.fromString(num.toString());
=> Long: -5130977905315463616
I don't understand why this is huge difference and why this is returning negative value.
13315766168394088000
is greater thanNumber.MAX_SAFE_INTEGER
(2^53), so you should not represent it with aNumber
(64-bit IEEE-754 double) and you shouldn't useLong.fromInt()
.You could work with either the bits or the decimal string representation of the value: