简体   繁体   中英

How Do I convert very big String to number in Java

Hi I have a big string like this :

"999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"

I wish to convert this string to long. But I failed. I did:

Long.parseLong(longString);

But I'm getting an error:

java.lang.NumberFormatException: For input string: "99999999.......

Are there any way to avoid this?

像这样使用BigInteger类:

 BigInteger number = new BigInteger(longString);

You need to use BigInteger . Long may not accommodate that big number.

Here is javadoc for BigInteger .

you might use BigInteger rather than Long.

 BigInteger number = new BigInteger(longString);

Java BigInteger example

long: Reference

use 8-byte to store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

As mention before answer, use BigInteger .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM