Santron Manibharathi
Santron Manibharathi

Reputation: 642

Prevent JavaScript Number function from rounding big numbers

I have a string value ' 9223372036854775807 '. I use Number() function in JavaScript to convert it to a number value using the following Code

var numericVal = Number(' 9223372036854775807 ');

when i check the value in numericVal, it is 9223372036854776000. How can I get the equivalent value for the string representing the Number?

Upvotes: 8

Views: 11816

Answers (4)

Dave Lasley
Dave Lasley

Reputation: 6242

Ordinary JavaScript numbers are Double Precision Floats; the largest integer that can be precisely stored is 253 (9007199254740992).

If you need to deal precisely with such large numbers, you can either:

  • Use the BigInt data type. Create one with BigInt("9223372036854775807") or 9223372036854775807n. Note that BigInt did not exist when this question was asked, but is now classed as "widely available" by MDN, which says it has "been available across browsers since September 2020". Or...
  • Use a library such as big.js. (This will give better support for ancient environments.)

Upvotes: 6

alexey28
alexey28

Reputation: 5220

The problem why Number('9223372036854775807') produce 9223372036854776000 is that JavaScript have limited precision (up to 16 digits) and you need 19 digits.

One of solutions might be using of java script big-numbers library:

// Initializa library:
var bn = new BigNumbers();

// Create numbers to compare:
var baseNumber = bn.of('9223372036854775807');
var toCompareNumber = bn.of('9223372036854775808');

// Compare:
if(baseNumber.lessOrEquals(toCompareNumber)) {
    console.log('This should be logged');
} else {
    console.log('This should NOT be logged');
}

Upvotes: 0

kennebec
kennebec

Reputation: 104760

You can compare strings that represent big integers as strings-

a longer string of integers is larger, otherwise compare characters in order.

You can sort an array of integer-strings

function compareBigInts(a, b){
    if(a.length== b.length) return a>b? 1:-1;
    return a.length-b.length;
}

or return the larger of two strings of digits

function getBiggestBigInts(a, b){
    if(a.length== b.length) return a>b? a:b;
    return a.length>b.length? a: b;
}

//examples

var n1= '9223372036854775807', n2= '9223372056854775807',
n3= '9223',n2= '9223372056854775817',n4= '9223372056854775';

getBiggestBigInts(n1,n2);>> 9223372056854775807

[n1,n2,n3,n4].sort(compareBigInts);>>

9223
9223372056854775
9223372036854775807
9223372056854775817

Just make sure you are comparing strings.

(If you use '-' minus values,a 'bigger' string value is less)

By the way,you sort big decimals by splitting on the decimal point and comparing the integer parts. If the integers are the same length and are equal, look at the the decimal parts.

Upvotes: 3

accme
accme

Reputation: 383

It's seems that your number is greater than 2^53, biggest integer number in javascript which can be represented without loosing precision (see this question).

If you really need to operate big numbers, you could use special libraries like this one: https://github.com/peterolson/BigInteger.js

Upvotes: 1

Related Questions