BigInteger.valueOf() 对于非常大的数字?

发布于 2024-08-03 02:50:50 字数 92 浏览 2 评论 0原文

在 Java 中将 50 位字符串转换为 BigInteger 的最佳方法是什么?它没有 valueOf(String) 方法,而且我无法转换为 Long,因为它太小了。

What would be the best way to convert a 50-digit String to a BigInteger in Java? It doesn't have a valueOf(String) method, and I can't convert to Long because it's too small.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤芳又自赏 2024-08-10 02:50:50

它确实有一个 BigInteger(String) 构造函数:-)

String S = "12345678901234567890123456789012345678901234567890";
BigInteger bi = new BigInteger(S);

It does have a BigInteger(String) constructor :-)

String S = "12345678901234567890123456789012345678901234567890";
BigInteger bi = new BigInteger(S);
非要怀念 2024-08-10 02:50:50

怎么样...

BigInteger bi = new BigInteger(my50DigitString);

所有这些 Xxx.valueOf() 方法都是构造函数的替代方法,因为它们允许返回共享的缓存副本。根据定义,构造函数每次都会返回一个新实例。所以 valueOf() 是一个很好的优化,但设计者显然没有发现提供 BigInteger.valueOf(String) 方法有趣。在这种情况下,您必须使用其中一个构造函数。

How about...

BigInteger bi = new BigInteger(my50DigitString);

All those Xxx.valueOf() methods are alternatives to constructors because they allow for returning shared, cached copies. Constructors, by definition, return a new instance every time. So valueOf() are a nice optimization, but the designers apparently didn't find it interesting to provide a BigInteger.valueOf(String) method. You'll have to use a one of the constructors in this case.

不回头走下去 2024-08-10 02:50:50

BigInteger 有一个接受 String 的构造函数。

BigInteger has a constructor that takes a String.

习ぎ惯性依靠 2024-08-10 02:50:50

你尝试过吗

BigInteger i = new BigInteger(s);

Have you tried

BigInteger i = new BigInteger(s);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文