Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript parsing int64

How can I convert a long integer (as a string) to a numerical format in Javascript without javascript rounding it?

var ThisInt = '9223372036854775808'
alert(ThisInt+'\r' +parseFloat(ThisInt).toString()+'\r' +parseInt(ThisInt).toString());

I need to perform an addition on it before casting it back as a string & would prefer not to have to slice it two if at all possible.

like image 252
Oppdal Avatar asked Mar 18 '11 14:03

Oppdal


People also ask

Can JavaScript handle int64?

JavaScript Numbers are represented as IEEE 754 double-precision floats. Unfortunately, this means they lose integer precision for values beyond +/- 2^^53. For projects that need to accurately handle 64-bit ints, such as node-thrift, a performant, Number-like class is needed.

Does JavaScript support 64-bit?

JavaScript Numbers are Always 64-bit Floating Point Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.

How do I use parseInt in JavaScript?

The parseInt method parses a value as a string and returns the first integer. A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal. If radix is omitted, JavaScript assumes radix 10.

What is JavaScript parseFloat?

The parseFloat() function is used to accept the string and convert it into a floating-point number. If the string does not contain a numeral value or If the first character of the string is not a Number then it returns NaN i.e, not a number.


3 Answers

All Numbers in Javascript are 64 bit "double" precision IEE754 floating point.

The largest positive whole number that can therefore be accurately represented is 2^53 - 1. The remaining bits are reserved for the exponent.

Your number is exactly 1024 times larger than that, so loses 3 decimal digits of precision. It simply cannot be represented any more accurately.

In ES6 one can use Number.isSafeInteger( # ) to test a number to see if its within the safe range:

var ThisInt = '9223372036854775808'; 
console.log( Number.isSafeInteger( parseInt( ThisInt ) ) );

There is also a BigInteger library available which should be able to help, though, and avoid you having to do all the string and bit twiddling yourself.

EDIT 2018/12 there's now a native BigInt class (and new literal syntax) landed in Chrome and NodeJS.

like image 142
Alnitak Avatar answered Oct 03 '22 05:10

Alnitak


With a little help from recursion, you can directly increment your decimal string, be it representing a 64 bit number or more...

/**
 * Increment a decimal by 1
 *
 * @param {String} n The decimal string
 * @return The incremented value
 */
function increment(n) {
    var lastChar = parseInt(n.charAt(n.length - 1)),
        firstPart = n.substr(0, n.length - 1);

    return lastChar < 9
        ? firstPart + (lastChar + 1)
        : firstPart
            ? increment(firstPart) + "0"
            : "10";
}
like image 45
Ates Goral Avatar answered Oct 03 '22 04:10

Ates Goral


You cannot do this with standard Javascript. But as always, there is a nifty little library to help us out, in this case BigInt.js, which will let you use arbitrary-precision integers.

like image 21
fresskoma Avatar answered Oct 03 '22 06:10

fresskoma