Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 870
sriram Avatar asked Nov 29 '25 06:11

sriram


2 Answers

Use BigInteger class like this:

 BigInteger number = new BigInteger(longString);
like image 123
higuaro Avatar answered Nov 30 '25 18:11

higuaro


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

Here is javadoc for BigInteger.

like image 35
kosa Avatar answered Nov 30 '25 18:11

kosa