Convert string value to float value

ReturnMethodSummary
static floatparseFloat(String s)Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.
static FloatvalueOf(float f)Returns a Float instance representing the specified float value.
static FloatvalueOf(String s)Returns a Float object holding the float value represented by the argument string s.

public class Main {
  public static void main(String[] args) {
    Float floatObject2 = Float.parseFloat("1.1F");
    System.out.println(floatObject2);
    
  }
}

The output:

1.1

You cannot use floating-point literal as Java think it is a double.


public class Main {
  public static void main(String[] args) {
    Float floatObject2 = Float.valueOf(1.1);
    System.out.println(floatObject2);
    
  }
}

When compiling it, the compile generates error message:


Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method valueOf(String) in the type Float is not applicable for the arguments (double)

    at Main.main(Main.java:3)
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.