Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int to String conversion

 
Anandh Ramesh
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i have a variable of type "int". i want to convert it to a string. can anyone help me on how to do it?

any help is appreciated.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i = 3;
String str = new Integer(i).toString();
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other way to do this:

 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran Pai's way is more prefered than the way that I have mentioned.As with his code we are not creating any object ,rather using a static helper finction of String class.
 
Anandh Ramesh
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks friends
 
Dave Lenton
Ranch Hand
Posts: 1241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
Jaikiran Pai's way is more prefered than the way that I have mentioned.As with his code we are not creating any object ,rather using a static helper finction of String class.


Also, if the type of i changes (for example to a long), the line of code doing the conversion will not need to change.

There's a good article on String conversion here:
http://www.yoda.arachsys.com/java/stringconv.html
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you have a huge number and you want to include the thousand separators. I believe that you can use a NumberFormatter object to do this conversion to a String, but I don't know how.

-- Kaydell
 
Leange Hsiung
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about the method:

int i = 1;
String str = ""+i;

Can someone explain the difficents with above?
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Dave Lenton, mentioned a link in his post earlier. Have a look at that, it answers your question.
 
Leange Hsiung
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it , Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic