Convert String to Float - Java Language Basics

Java examples for Language Basics:float

Description

Convert String to Float

Demo Code


public class Main {
 
  public static void main(String[] args) {
    //1. Construct Float using constructor.
    Float fObj1 = new Float("10.4");
    System.out.println(fObj1);//from www  .  jav a 2 s .  com
   
    //2. Use valueOf method of Float class. This method is static.
    String str1 = "1.4";
    Float fObj2 = Float.valueOf(str1);
    System.out.println(fObj2);
   
    String str2 = "7.9";
    float f = Float.parseFloat(str2);
    System.out.println(f);
  }
}

Result


Related Tutorials