Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int to BigInteger

 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone let me know how to convert an 'int' to BigInteger.

---------------------------
Thanks in Advance
[ October 13, 2005: Message edited by: S Thanigaivel ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15276
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private static BigInteger xyz = BigInteger.valueOf(3);
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And both approaches could have been found easily by spending a few minutes looking over BigInteger's javadocs.
 
Campbell Ritchie
Sheriff
Posts: 48930
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));

Ready to do anything...
 
Campbell Ritchie
Sheriff
Posts: 48930
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Did you notice that a better solution had been suggested over 8 years ago?
 
Liesbert Garcia Moreno
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic