Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# int to BigInteger

S Thanigaivel
Ranch Hand
Posts: 60
Can anyone let me know how to convert an 'int' to BigInteger.

---------------------------
[ October 13, 2005: Message edited by: S Thanigaivel ]

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15568
46
You probably already discovered that BigInteger does not have a constructor that takes an int. It does, however, have a constructor that takes a String. So all you have to do is to convert your int to a String and pass that to the constructor.
[ October 13, 2005: Message edited by: Jesper de Jong ]

Jeff Albertson
Ranch Hand
Posts: 1780
The route suggested by the previous poster, int -> String -> BigInteger,
seems circuitous to this old cowpoke. Try the following BigInteger method:

For example:

Michael Kammerhofer
Greenhorn
Posts: 1
private static BigInteger xyz = BigInteger.valueOf(3);

Jeff Verdegan
Bartender
Posts: 6109
6
And both approaches could have been found easily by spending a few minutes looking over BigInteger's javadocs.

Campbell Ritchie
Sheriff
Posts: 50702
83
Welcome to the Ranch Michael Kammerhofer. Why are you suggesting that field be static? Please search this forum for threads about the difference between static and non-static.

Liesbert Garcia Moreno
Greenhorn
Posts: 2
Jesper de Jong wrote:You probably already discovered that BigInteger does not have a constructor that takes an int. It does, however, have a constructor that takes a String. So all you have to do is to convert your int to a String and pass that to the constructor.
[ October 13, 2005: Message edited by: Jesper de Jong ]

I'm so agree with you. This'll be the way.

int x = 5;
BigInteger temp = new BigInteger(Integer.toString(x));

Campbell Ritchie
Sheriff
Posts: 50702
83
Welcome to the Ranch

Did you notice that a better solution had been suggested over 8 years ago?

Liesbert Garcia Moreno
Greenhorn
Posts: 2
Campbell Ritchie wrote:Welcome to the Ranch

Did you notice that a better solution had been suggested over 8 years ago?

Well, users are looking for information every day, I just want to help new users. I'm sorry if I did something wrong.