Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-conversion Convert String to Number

Related Tutorials


 
 

Share on Google+Share on Google+

Convert String to Number

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

Convert String to Number

     

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

Code Description:

In this program we have taken a String of int, byte, boolean, double as shown below and also we have used the method MIN
_VALUE and MAX_VALUE of different wrapper classes to print all the values on int, short, double etc. 

Here is the code of the program:

public class StrToNum{
  public static void main (String args[]){
  String table = "Table of the Java Numbers\n" +
 "Primitive Type\tSize in Bits\tTypical Value\tMinimum Value
\tMaximum Value\n" 
+
 "**************\t**************\t**************\t**********
****\t**************\n"
;
  
  // int
  table += "int\t\t" 32 "\t\t" 65536 "\t\t" + Integer.MIN_VALUE
 + 
"\t" + Integer.MAX_VALUE + "\n";
  // byte
  table += "byte\t\t" "\t\t" (byte)"\t\t" + Byte.MIN_VALUE 
"\t\t" + Byte.MAX_VALUE + "\n";
  // short
  table += "short\t\t" 16 "\t\t" (short)1024 "\t\t" + Short.MIN_VALUE
 + 
"\t\t" + Short.MAX_VALUE + "\n";
  //boolean
  table += "boolean\t\t" "\t\t" true ',' false "\t\t" "\n";
  // double
  table += "double\t\t" 64 "\t\t" 100e100 "\t\t" + Double.MIN_VALUE +
 
"\t" + Double.MAX_VALUE + "\n";
  table +=
 "**************\t**************\t**************\t***************\t************\n";

  System.out.println(table);
}
}

Output of the program:

C:\unique>javac StrToNum.java

C:\unique>java StrToNum
Formatable of the Java Numbers
Primitive Type   Size in Bits  Typical Value  Minimum Value   Maximum Value
************** ************** ************** ************** **************
int   32  65536   -2147483648  2147483647
byte  8  5   -128  127
short  16  1024  -32768  32767
boolean   true,false
double   64  1.0E102   4.9E-324   1.79769313486231
57E308
************** ************** ************** *************** ************

C:\unique>

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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: May 24, 2009

Related Tutorials

Discuss: Convert String to Number  

Post your Comment


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