How do I convert a String to a BigInteger?

前端 未结 6 1416
自闭症患者
自闭症患者 2020-11-30 08:09

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);

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 09:06

    According to the documentation:

    BigInteger(String val)

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

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

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

提交回复
热议问题