集册 Java实例教程 将String转换为Float

将String转换为Float

欢马劈雪     最近更新时间:2020-01-02 10:19:05

634
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将String转换为Float
/*来自 
 NowJava.com*/


public class Main {

 

  public static void main(String[] args) {

    //1. Construct Float using constructor.

    Float fObj1 = new Float("10.4");

    System.out.println(fObj1);

   

    //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);

  }

}