BigInteger.valueOf() for very big numbers?

瘦欲@ 提交于 2019-12-12 14:45:02

问题


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.


回答1:


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

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



回答2:


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.




回答3:


BigInteger has a constructor that takes a String.




回答4:


Have you tried

BigInteger i = new BigInteger(s);


来源:https://stackoverflow.com/questions/1343982/biginteger-valueof-for-very-big-numbers

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