问题
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?
回答1:
Use BigInteger class like this:
BigInteger number = new BigInteger(longString);
回答2:
You need to use BigInteger
. Long
may not accommodate that big number.
Here is javadoc for BigInteger.
回答3:
you might use BigInteger rather than Long.
BigInteger number = new BigInteger(longString);
Java BigInteger example
回答4:
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
.
来源:https://stackoverflow.com/questions/12967201/how-do-i-convert-very-big-string-to-number-in-java