在Javascript中将字符串转换为大整数?

6

我正在尝试将一个字符串转换为大整数以进行一些算术计算。然而,当我尝试这样做:

Number("9007199254740993")

我得到了这个意外的结果:

9007199254740992

我怀疑这可能是由于Number能够处理的整数大小限制所致。
基本上,我想检查两个字符串是否连续。由于Number没有返回正确的值,我得到了"9007199254740993""9007199254740992"之间的错误差值。具体来说,我期望得到1,但实际上却得到了0。
我考虑过的一个可能性是将每个数字除以因子以使它们变小。还有其他解决方案吗?

3
"BigInt"(第3阶段提案),是一个“大数”库,或者根据您的需求可以不将它们转换为实数。翻译完毕,无返回内容。 - Andreas
请参见以下链接:https://dev59.com/dKTja4cB1Zd3GeqPAVk1 - Chris Happy
@ChrisHappy。感谢提供链接。我搜索了是否已经有类似的问题,但没有找到。 - Vikram Kumar
@VikramKumar,我已经更新了我的代码。 - Niraj Kaushal
4个回答

3

Javascript的Number类型是一个双精度64位浮点格式(IEEE 754)的数值数据类型

如果您需要处理大整数,请使用BigInt或相应的库。


感谢您在BigInt方面的提示。希望它能很快被所有浏览器所支持。 - Vikram Kumar

1

注意:如果你要处理大数,我建议你使用BigInt(由@Andreas在评论中提出)。


已更新

使用以下代码比较大的正整数(参数应为字符串格式)

function compareBigNumber(num1, num2) {
  if (num1 > Number.MAX_SAFE_INTEGER && num2 > Number.MAX_SAFE_INTEGER) {
    var newNum1 = num1.split('').reverse();
    var newNum2 = num2.split('').reverse();
  
    do {
      newNum1.pop();
      newNum2.pop();
    } while (newNum1[newNum1.length-1] === '0' || newNum2[newNum2.length-1] === '0')
    
    return compareBigNumber(newNum1.reverse().join(''), newNum2.reverse().join(''));
  } else if(num1 > Number.MAX_SAFE_INTEGER){ 
    return 'num1 is greater' 
  } else if (num2 > Number.MAX_SAFE_INTEGER) {
    return 'num2 is greater'
  }
  else {
    var num1Int = parseInt(num1);
    var num2Int = parseInt(num2);
    if (num1Int > num2Int) {
      return 'Num1 is greater';
    } else if (num2Int > num1Int){
      return 'Num2 is greater'
    } else {
      return 'Num1 is equal to Num2';
    }
  }
}

console.log(compareBigNumber("9007199254740992", "9007199254740993"))
console.log(compareBigNumber("100000000000000000000", "0"))


@Vikram Kumar。这里的答案是错误的(也许在你的情况下可以工作,但不能推广),因为如果您取了数字90071992547409921和90071992547409922,那么答案将相同(因为/10后两个数字都给出相同的数字)。 - Rajan Lagah
compareBigNumber("1000000000000000000000", "0") 返回 "Num1 等于 Num2"。看起来你对代码做了一些假设,但在这种情况下不成立。 - FK82

1

如果你不想依赖于BigInt,并且只考虑正整数,那么你也可以自己编写后继测试。完整代码如下所示。


笔记

正整数的字符串表示很容易转换为十进制数组,其中索引表示以10为底数的指数。例如"42" ~> [2, 4](因为42 = 2*10^0 + 4*10^1)。您也可以轻松地将其转换回来。

现在,对于后继测试,您只需要定义增量操作(即带进位的加1)。有了这个,您就可以比较一个数字的增量是否等于未增量的另一个数字(反之亦然)。


代码

// Convert a string representation of positive decimal integer to an array of decimals.
const toArray = numberString => Array.from(numberString, c => parseInt(c))
    .reverse();

// Convert the array representation of a positive decimal integer string back to the corresponding string representation (this is the inverse of `toArray`).
const fromArray = numberArray => numberArray.map(String)
    .reverse()
    .join('');

console.log(fromArray(toArray("9007199254740993")) === "9007199254740993"); // true

// Perform the increment operation on the array representation of the positive decimal integer.
const increment = numberArray => {
  let carry = 1;
  const incrementedNumberArray = [];
  numberArray.forEach(i => {
      let j;
      if (carry === 0) {
          j = i;
      } else if (carry === 1) {
          if (i === 9) {
              j = 0;
          } else {
              j = i + 1;
              carry = 0;
          }
      }
      incrementedNumberArray.push(j);
  });

  if (carry === 1) { 
    incrementedNumberArray.push(1);
  }

  return incrementedNumberArray;
};

console.log(fromArray(increment(toArray("9007199254740993"))) === "9007199254740994"); // true
console.log(fromArray(increment(toArray("9999999999999999"))) === "10000000000000000"); // true

// Test if two strings represent positive integers where one is the other's successor.  
const isSuccessor = (a, b) => {
  const a_ = increment(toArray(a));
  const b_ = increment(toArray(b));
  return fromArray(a_) === b || fromArray(b_) === a;
};

console.log(isSuccessor("9007199254740993", "9007199254740994")); // true
console.log(isSuccessor("9007199254740994", "9007199254740993")); // true
console.log(isSuccessor("9999999999999999", "10000000000000000")); // true
console.log(isSuccessor("10000000000000000", "9999999999999999")); // true
console.log(isSuccessor("10000000000000000", "10000000000000002")); // false


1
你可以使用类似JAVA中的大整数库。 点击这里查看
npm install big-integer

var bigInt = require("big-integer");
var largeNumber1 = bigInt("9007199254740993");
var largeNumber2 = bigInt("9007199254740994"); // any other number
var ans = largeNumber1.minus(largeNumber2);
if(ans == 1 || ans == -1){
console.log('consecutive ')
}else{
console.log('not consecutive ')
}

感谢提供big-integer库。不幸的是,我正在寻找一种本地解决方案(不能包含第三方库)。我现在将实施Neeraj的解决方案。 - Vikram Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接