【发布时间】:2013-03-20 23:32:12
【问题描述】:
我正在尝试从标准输入中读取一些非常大的数字并将它们相加。
但是,要添加到 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)之类的东西吗?
【问题讨论】:
标签: java string biginteger