我想从标准输入中读取一些非常大的数字并将它们添加在一起。如何将字符串转换为BigInteger?
然而,添加到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)));
}
这工作正常,但作为BigInteger.valueOf()
只需要long
,我不能添加数字比long
的最高值(9223372036854775807 )。
每当我尝试添加9223372036854775808或更多,我得到一个NumberFormatException(这是完全预期的)。
有没有类似BigInteger.parseBigInteger(String)
?