Syntax error Convert from String to float in Java

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
Updated on: 2020-06-26T08:48:08+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements