• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting Integers / Float/ Double etc to strings  RSS feed

 
Tim Dee
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know how to convert a string to Float / int etc

But how do I go in the other direction and convert a integer to a string?

Cheers
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of ways to do it. I like to use the bunch of static "valueOf" methods in the String class:

int anInt = 3;
String intAsString = String.valueOf(anInt);
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of different ways to do it.

Check out the API documentation of class java.text.DecimalFormat and of the method String.format(...). With those, you can specify exactly with which format you want the numbers to be converted to strings.

You can also use the static toString(...) methods of the wrapper classes, but those won't allow you to specify the format.

Or even a trick like this, which will automatically call the toString(...) of the wrapper classes behind the scenes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!