How Do I convert very big String to number in Java

限于喜欢 提交于 2019-12-12 03:44:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!