如何将 Java String 对象转换为 float

将 String 转换为 float,你需要先将 String 转换为 Float 对象,然后将 Float 对象转换为数据类型。

可以参考下下面的程序代码:[code]public class StringToFloat
{

public static void main (String[] args)
{

// String s = "fred";    // do this if you want an exception

String s = "100.00";

try
{
  float f = Float.valueOf(s.trim()).floatValue();
  System.out.println("float f = " + f);
}
catch (NumberFormatException nfe)
{
  System.out.println("NumberFormatException: " + nfe.getMessage());
}

}
}[/code]