[英]How Do I convert very big String to number in Java
Hi I have a big string like this : 嗨,我有一个很大的字符串,像这样:
"999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999" “ 999999999999999999999999999999999999999999999999999999999999999999999999999999999999
I wish to convert this string to long. 我希望将此字符串转换为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
. 您需要使用BigInteger
。 Long
may not accommodate that big number. Long
可能无法容纳这么大的数字。
Here is javadoc for BigInteger . 这是BigInteger的 javadoc。
you might use BigInteger rather than Long. 您可以使用BigInteger而不是Long。
BigInteger number = new BigInteger(longString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.