Convert from String to float in Java

Java 8Object Oriented ProgrammingProgramming

To convert String to float, use the valueOf() method.

Let’s say we have the following string value.

String str = "0.8";

Converting the string to float.

Float floatVal = Float.valueOf(str).floatValue();

The following is the complete example.

Example

 Live Demo

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

Output

Float: 0.8
raja
Published on 10-Dec-2018 12:03:58
Advertisements