简体   繁体   English

如何在Java中将很大的String转换为数字

[英]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);

Java BigInteger example Java BigInteger示例

long: Reference long: 参考

use 8-byte to store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 使用8字节存储从-9,223,372,036,854,775,8089,223,372,036,854,775,807

As mention before answer, use BigInteger . 如答案前所述,请使用BigInteger

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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