Java 从整数转换为 BigInteger

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3878192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 06:14:52  来源:igfitidea点击:

Converting from Integer, to BigInteger

javabiginteger

提问by Steffan Harris

I was wondering if there was any way to convert a variable of type Integer, to BigInteger. I tried typecasting the Integer variable, but i get an error that says inconvertible type.

我想知道是否有任何方法可以将 Integer 类型的变量转换为 BigInteger。我尝试对 Integer 变量进行类型转换,但我收到一个错误,指出类型不可转换。

采纳答案by jbindel

The method you want is BigInteger#valueOf(long val).

您想要的方法是BigInteger#valueOf(long val)

E.g.,

例如,

BigInteger bi = BigInteger.valueOf(myInteger.intValue());

Making a String first is unnecessary and undesired.

首先创建一个字符串是不必要的和不受欢迎的。

回答by Giorgios Karagounis

You can do in this way:

你可以这样做:

    Integer i = 1;
    new BigInteger("" + i);