Java Program to convert a String to a float type Number

Java 8Object Oriented ProgrammingProgramming

Use the parseFloat() method in Java to convert a String to a float type.

Let’s say we have the following string.

String str = "111.8";

Now, the Float.parseFloat() method will convert it to a float type number.

float floatVal = Float.parseFloat(str);

The following is the complete example with output.

Example

 Live Demo

public class Demo {
   public static void main(String args[]) {
      String str = "111.8";
      float floatVal = Float.parseFloat(str);
      System.out.println("Float: "+floatVal);
   }
}

Output

Float: 111.8
raja
Published on 10-Dec-2018 12:06:06
Advertisements