Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Convert String Value to Float Using Float.valueOf in Java



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: 2020-06-26T08:13:31+05:30

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements