Given a string and we have to convert it into a float.
给定一个字符串,我们必须将其转换为浮点数。
Java conversion from String to Float
Java从String转换为Float
To convert a String to Float, we can use the following methods of Float class (see the syntax given below...)
要将String转换为Float ,我们可以使用Float类的以下方法(请参见下面给出的语法...)
Syntax:
句法:
Float Float.valueOf(String).floatValue();
OR
Float Float.parseFloat(String);
Java代码将字符串转换为Float (Java code to convert a String to Float)
//Java code to convert String to Float
public class Main {
public static void main(String args[]) {
String str = "1234.587878f";
//variable to store result
float result = 0;
//converting string to Float
//method 1
result = Float.valueOf(str).floatValue();
System.out.println("result (value of str as float) = " + result);
//method 2
result = Float.parseFloat(str);
System.out.println("result (value of str as float) = " + result);
}
}
Output
输出量
result (value of str as float) = 1234.5879
result (value of str as float) = 1234.5879
翻译自: https://www.includehelp.com/java-programs/conversion-from-string-to-float-in-java.aspx