8

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?

Santron Manibharathi
  • 628
  • 5
  • 12
  • 26
  • http://stackoverflow.com/questions/1725341/javascript-large-number-library – Rob W Mar 20 '13 at 16:38
  • my value should be between the range -9223372036854775808 and 9223372036854775807 hence i used the below code `if(valueToCompare.startsWith('-')) { valueToCompare = valueToCompare.substring(1); isValid = !(valueToCompare>9223372036854775808); } else { isValid = !(valueToCompare>9223372036854775807); }` – Santron Manibharathi Mar 23 '13 at 13:19
  • possible duplicate of [How to deal with big numbers in javascript](http://stackoverflow.com/questions/4288821/how-to-deal-with-big-numbers-in-javascript) – Bergi Aug 26 '13 at 20:55

4 Answers4

6

JavaScript numbers are Double Precision Floats; the largest integer that can be precisely stored is 2^53 (9007199254740992). If you actually need it in a number you have some fun math ahead of you, or you can use a library such as big.js

Dave Lasley
  • 5,262
  • 1
  • 34
  • 37
  • I am quite confused as the Number which came in output was bigger than the number i gave. If it can store bigger value, can't it store smaller values? – Santron Manibharathi Mar 20 '13 at 16:42
  • Sorry, I mis-pasted - 2^53 is 9007199254740992. It can store higher values, just not precisely. – Dave Lasley Mar 20 '13 at 16:44
  • But 9223372036854776000 is greater than 9007199254740992 right. How is this value being stored? – Santron Manibharathi Mar 20 '13 at 16:49
  • 1
    You are correct that it is a higher value, a `Double Precision Float` can only be precise between -2^53 and 2^53. JavaScript will attempt to store the number, but it will not be able to be precise due to the object type. Also note that JavaScript arithmetic operators work in 64-bit, but bitwise and shift operators work in 32-bits. If you are working with numbers of this size, you will need to use a non-native object such as big.js – Dave Lasley Mar 20 '13 at 17:00
3

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.

kennebec
  • 102,654
  • 32
  • 106
  • 127
1

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

Community
  • 1
  • 1
accme
  • 383
  • 1
  • 2
  • 11
0

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');
}
alexey28
  • 5,170
  • 1
  • 20
  • 25