All for Joomla All for Webmasters

Java String to float

We can convert String to float in java using Float.parseFloat() method.

Scenario

It is generally used if we have to perform mathematical operations on the string that contains float number. Whenever we get data from textfield or textarea, entered data is received as a string. If entered data is float, we need to convert string to float. To do so, we use Float.parseFloat() method.

Signature

The parseFloat() is the static method of Float class. The signature of parseFloat() method is given below:

  1. public static int parseFloat(String s)

Java String to float Example

Let’s see the simple code to convert string to float in java.

  1. float f=Float.parseFloat(“23.6”);

Let’s see the simple example of converting String to float in java.

  1. public class StringToFloatExample{
  2. public static void main(String args[]){
  3. String s=“23.6”;
  4. float f=Float.parseFloat(“23.6”);
  5. System.out.println(f);
  6. }}

Output:

23.6
PinIt
submit to reddit

Leave a Reply

Top