如何将String转换为BigInteger?

时间:2021-03-24 16:54:33

I'm trying to read some really big numbers from standard input and add them together.

我试图从标准输入读取一些非常大的数字并将它们加在一起。

However, to add to BigInteger, I need to use BigInteger.valueOf(long);:

但是,要添加到BigInteger,我需要使用BigInteger.valueOf(long);:

private BigInteger sum = BigInteger.valueOf(0);

private void sum(String newNumber) {
    // BigInteger is immutable, reassign the variable:
    sum = sum.add(BigInteger.valueOf(Long.parseLong(newNumber)));
}

That works fine, but as the BigInteger.valueOf() only takes a long, I cannot add numbers greater than long's max value (9223372036854775807).

这工作正常,但由于BigInteger.valueOf()只需要很长时间,我不能添加大于long的最大值的数字(9223372036854775807)。

Whenever I try to add 9223372036854775808 or more, I get a NumberFormatException (which is completely expected).

每当我尝试添加9223372036854775808或更多时,我都会得到一个NumberFormatException(完全可以预料到)。

Is there something like BigInteger.parseBigInteger(String)?

有没有类似BigInteger.parseBigInteger(String)的东西?

5 个解决方案

#1


91  

Using the constructor

使用构造函数

BigInteger(String val)

BigInteger(String val)

Translates the decimal String representation of a BigInteger into a BigInteger.

将BigInteger的十进制字符串表示形式转换为BigInteger。

Javadoc

的Javadoc

#2


16  

According to the documentation:

根据文件:

BigInteger(String val)

BigInteger(String val)

Translates the decimal String representation of a BigInteger into a BigInteger.

将BigInteger的十进制字符串表示形式转换为BigInteger。

It means that you can use a String to initialize a BigInteger object, as shown in the following snippet:

这意味着您可以使用String初始化BigInteger对象,如以下代码段所示:

sum = sum.add(new BigInteger(newNumber));

#3


8  

BigInteger has constructor where you can pass string as argument.

BigInteger有构造函数,您可以在其中传递字符串作为参数。

try below,

试试下面,

private void sum(String newNumber) {
    // BigInteger is immutable, reassign the variable:
    sum = sum.add(new BigInteger(newNumber));
}

#4


6  

Instead of using valueOf(long) and parse(), you can directly use the BigInteger constructor that takes a string argument:

您可以直接使用带有字符串参数的BigInteger构造函数,而不是使用valueOf(long)和parse()。

BigInteger numBig = new BigInteger("8599825996872482982482982252524684268426846846846846849848418418414141841841984219848941984218942894298421984286289228927948728929829");

That should give you the desired value.

这应该给你想要的价值。

#5


0  

For a loop where you want to convert an array of strings to an array of bigIntegers do this:

对于要将字符串数组转换为bigIntegers数组的循环,请执行以下操作:

String[] unsorted = new String[n]; //array of Strings
BigInteger[] series = new BigInteger[n]; //array of BigIntegers

for(int i=0; i<n; i++){
    series[i] = new BigInteger(unsorted[i]); //convert String to bigInteger
}

#1


91  

Using the constructor

使用构造函数

BigInteger(String val)

BigInteger(String val)

Translates the decimal String representation of a BigInteger into a BigInteger.

将BigInteger的十进制字符串表示形式转换为BigInteger。

Javadoc

的Javadoc

#2


16  

According to the documentation:

根据文件:

BigInteger(String val)

BigInteger(String val)

Translates the decimal String representation of a BigInteger into a BigInteger.

将BigInteger的十进制字符串表示形式转换为BigInteger。

It means that you can use a String to initialize a BigInteger object, as shown in the following snippet:

这意味着您可以使用String初始化BigInteger对象,如以下代码段所示:

sum = sum.add(new BigInteger(newNumber));

#3


8  

BigInteger has constructor where you can pass string as argument.

BigInteger有构造函数,您可以在其中传递字符串作为参数。

try below,

试试下面,

private void sum(String newNumber) {
    // BigInteger is immutable, reassign the variable:
    sum = sum.add(new BigInteger(newNumber));
}

#4


6  

Instead of using valueOf(long) and parse(), you can directly use the BigInteger constructor that takes a string argument:

您可以直接使用带有字符串参数的BigInteger构造函数,而不是使用valueOf(long)和parse()。

BigInteger numBig = new BigInteger("8599825996872482982482982252524684268426846846846846849848418418414141841841984219848941984218942894298421984286289228927948728929829");

That should give you the desired value.

这应该给你想要的价值。

#5


0  

For a loop where you want to convert an array of strings to an array of bigIntegers do this:

对于要将字符串数组转换为bigIntegers数组的循环,请执行以下操作:

String[] unsorted = new String[n]; //array of Strings
BigInteger[] series = new BigInteger[n]; //array of BigIntegers

for(int i=0; i<n; i++){
    series[i] = new BigInteger(unsorted[i]); //convert String to bigInteger
}