File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Converting Integers / Float/ Double etc to strings Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Converting Integers / Float/ Double etc to strings" Watch "Converting Integers / Float/ Double etc to strings" New topic
Author

Converting Integers / Float/ Double etc to strings

Tim Dee
Greenhorn

Joined: May 07, 2005
Posts: 22
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
Marshal

Joined: Jul 08, 2003
Posts: 24041
    
  13

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);


[Jess in Action][AskingGoodQuestions]
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: Converting Integers / Float/ Double etc to strings
 
Similar Threads
floats and Float objects
bean:write format string as number
conversion from String to any data type
How to convert String to float with exact decimal value
string float conversion