Home Java Java-conversion Convert Number to String
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Convert Number to String

Advertisement
In this section, we will learn how to convert numbers to String.

Convert Number to String

     

In this section, we will learn how to convert numbers to String. The following program provides you the functionality to convert numbers to String.

Code Description:

In this program we have declared different type of  numbers like float, int, long etc as shown below. We have used "valueOf()" method here which is a method of String class. We use "String.valueOf()" for numeric conversions and "toString()" method to convert these numeric classes to String as shown in the code below.

Here is the code of the program:

class NumToStr {
  public static void main(String[] args) {
  int i  = 40;
  float f  = (float100.0;
  long l = 3000000;
  String s = new String();

  s = String.valueOf(i);
  System.out.println ("int i = " + s);
  s = String.valueOf(f);
  System.out.println ("float f = " + s);
  s = String.valueOf(l);
  System.out.println ("long l = " + s);
  s = new Integer(i).toString();
  System.out.println ("int i = " + s);
  s = new Float(f).toString();
  System.out.println ("float f = " + s);
  s = new Long(l).toString();
  System.out.println ("long l = " + s);
  }
}

Output of the program:

C:\unique>javac NumToStr.java

C:\unique>java NumToStr
int i = 40
float f = 100.0
long l = 3000000
int i = 40
float f = 100.0
long l = 3000000

C:\unique>

Download this example.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 9, 2007

Ask Questions?    Discuss: Convert Number to String   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com