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



Home Java Java-conversion Convert String To Float

Related Tutorials


 
 

Share on Google+Share on Google+

Convert String To Float

Advertisement
In this section, you will learn to convert a numeric type string value into a float.

Convert String To  Float 

     

In this section, you will learn to convert a numeric type string value into a float. 

Code Description:

This program takes a numeric type string at the console. The valueOf() method takes a string type parameter and returns a float object. And the floatValue() method returns the float value of this float object. Finally, we get the float type data.

Float.valueOf(): This is the method that takes a string type value and returns a float object. If the string is null, then a NullPointerException is thrown.

floatValue(): This method returns the float value of this float object.

Here is the code of this program:

import java.io.*;
import java.lang.*;

public class StringToFloat{
  public static void main (String[] argsthrows IOException{
  BufferedReader reader = 
 
new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter the numeric type string value:");
  String str = reader.readLine();
  try{
  float f = Float.valueOf(str.trim()).floatValue();
  System.out.println("Float value is: = " + f);
  }
  catch (Exception e){
  System.out.println("Exception: " + e.getMessage());
  }
  }
}

Download of this program:

Output of this program given below.

C:\corejava>java StringToFloat
Enter the numeric type string value:
50
Float value is: = 50.0
C:\corejava> _
Advertisements

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: July 5, 2007

Related Tutorials

Discuss: Convert String To Float  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
sam
October 12, 2011
java

to convert string value to float: String s="10"; float f=new Float(s); System.out.print(f);
DMCA.com