View RSS Feed

Java String

Convert Number to String

Rate this Entry
by , 04-26-2012 at 06:56 PM (459 Views)
This sections shows how numbers can be converted into String. Given program gives the functionality for conversion of numbers to String.

Code Description:
Different kind of numbers have been declared, for example, long, int and float etc given below. valueOf() method is used which is basically Stringclass’s method. For numeric conversions String.valueOf() is used & numeric classes are converted into String by using toString() method, as given below:
Program’s code is given below:

Java Code:
class NumToStr {
  public static void main(String[] args) {
  int i  = 40;
  float f  = (float) 100.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);
  }
}

Submit "Convert Number to String" to Facebook Submit "Convert Number to String" to Digg Submit "Convert Number to String" to del.icio.us Submit "Convert Number to String" to StumbleUpon Submit "Convert Number to String" to Google

Comments