Java Program to convert string value to float using Float.valueOf()


The Float.valueOf() method is used in Java to convert string to float.

The following are our string values.

String str1 = "100.5";
String str2 = "200.5";

To convert the string value to float, try the method valueOf()

float val1 = (Float.valueOf(str1)).floatValue();
float val2 = (Float.valueOf(str2)).floatValue();

The following is the complete example with output.

Example

 Live Demo

public class Demo {
   public static void main(String args[]) throws Exception {
      String str1 = "100.5";
      String str2 = "200.5";
      float val1 = (Float.valueOf(str1)).floatValue();
      float val2 = (Float.valueOf(str2)).floatValue();
      System.out.println("Multiplication = " + (val1 * val2));
   }
}

Output

Multiplication = 20150.25

Updated on: 26-Jun-2020

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements