0

我都试过了:

var a = new BigInteger(5);

var b = new BigInteger(5, 10);

但两者都给了我错误:

TypeError: 'undefined' is not an object (evaluating 'b.nextBytes')
bnpFromNumberjsbn2.js:126

你只能用字符串实例化吗?

4

1 回答 1

0

我想给出一个更好的答案,但你没有提到你正在使用哪个 BigInteger 库。

// Yes, use the two '..'
var a = new BigInteger(5..toString());

// Of if you have a variable
var v = 10;
var a = new BigInteger(v.toString());

现在,有了这些知识,您可以覆盖 BigInteger

(function() {
  var oldConstructor = BigInteger;
  BigInteger = function(v) {
    if (typeof v === "number") {
      return oldConstructor(""+v);
    }
    return oldConstructor(v);
  };

}());
于 2014-04-20T15:45:39.310 回答