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

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 10-Dec-2018 11:18:00
Advertisements