public static Float valueOf(String s)
throws NumberFormatExceptionReturns a Float object holding the float value represented by the argument string s. If s is null, then a NullPointerException is thrown. Leading and trailing whitespace characters in s are ignored. The rest of s should constitute a FloatValue as described by the lexical syntax rules: FloatValue:
Signopt NaNSignopt InfinitySignopt FloatingPointLiteralwhere Sign and FloatingPointLiteral are as defined in §3.10.2 of the Java Language Specification. If s does not have the form of a FloatValue, then a NumberFormatException is thrown. Otherwise, s is regarded as representing an exact decimal value in the usual "computerized scientific notation"; this exact decimal value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type float by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value. Finally, a Float object representing this float value is returned.To interpret localized string representations of a floating-point value, use subclasses of NumberFormat.
Parameters:
s - the string to be parsed.
Returns:
a Float object holding the value represented by the String argument.
Throws:
NumberFormatException - if the string does not contain a parsable number.
parseFloatpublic static float parseFloat(String s)
throws NumberFormatExceptionReturns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.
Parameters:
s - the string to be parsed.
Returns:
the float value represented by the string argument.
Throws:
NumberFormatException - if the string does not contain a parsable float.
Since:
1.2See Also:
valueOf(String)
throws NumberFormatExceptionReturns a Float object holding the float value represented by the argument string s. If s is null, then a NullPointerException is thrown. Leading and trailing whitespace characters in s are ignored. The rest of s should constitute a FloatValue as described by the lexical syntax rules: FloatValue:
Signopt NaNSignopt InfinitySignopt FloatingPointLiteralwhere Sign and FloatingPointLiteral are as defined in §3.10.2 of the Java Language Specification. If s does not have the form of a FloatValue, then a NumberFormatException is thrown. Otherwise, s is regarded as representing an exact decimal value in the usual "computerized scientific notation"; this exact decimal value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type float by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value. Finally, a Float object representing this float value is returned.To interpret localized string representations of a floating-point value, use subclasses of NumberFormat.
Parameters:
s - the string to be parsed.
Returns:
a Float object holding the value represented by the String argument.
Throws:
NumberFormatException - if the string does not contain a parsable number.
parseFloatpublic static float parseFloat(String s)
throws NumberFormatExceptionReturns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.
Parameters:
s - the string to be parsed.
Returns:
the float value represented by the string argument.
Throws:
NumberFormatException - if the string does not contain a parsable float.
Since:
1.2See Also:
valueOf(String)
float f=0;
try{
f=Float.parseFloat(s);}catch(Exception e){f=0;}
应该是
String s="3.12";
float f=0;
try{
f=Float.parseFloat(s).floatValue(); }catch(Exception e){f=0;}
throws NumberFormatException
都已经返回一个float值了 你这不是画蛇添足:)