express-cassandra is converting Long value into negative

175 views Asked by At

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.

1

There are 1 answers

1
jorgebg On

13315766168394088000 is greater than Number.MAX_SAFE_INTEGER (2^53), so you should not represent it with a Number (64-bit IEEE-754 double) and you shouldn't use Long.fromInt().

You could work with either the bits or the decimal string representation of the value:

const longValue = Long.fromString('13315766168394088000');