Convert from String to float in Java



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
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements