Megosztás a következőn keresztül:


Float.ValueOf Method

Definition

Overloads

ValueOf(Single)

Returns a Float instance representing the specified float value.

ValueOf(String)

Returns a Float object holding the float value represented by the argument string s.

ValueOf(Single)

Returns a Float instance representing the specified float value.

[Android.Runtime.Register("valueOf", "(F)Ljava/lang/Float;", "")]
public static Java.Lang.Float ValueOf (float f);
[<Android.Runtime.Register("valueOf", "(F)Ljava/lang/Float;", "")>]
static member ValueOf : single -> Java.Lang.Float

Parameters

f
Single

a float value.

Returns

a Float instance representing f.

Attributes

Remarks

Returns a Float instance representing the specified float value. If a new Float instance is not required, this method should generally be used in preference to the constructor #Float(float), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

Added in 1.5.

Java documentation for java.lang.Float.valueOf(float).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

ValueOf(String)

Returns a Float object holding the float value represented by the argument string s.

[Android.Runtime.Register("valueOf", "(Ljava/lang/String;)Ljava/lang/Float;", "")]
public static Java.Lang.Float ValueOf (string s);
[<Android.Runtime.Register("valueOf", "(Ljava/lang/String;)Ljava/lang/Float;", "")>]
static member ValueOf : string -> Java.Lang.Float

Parameters

s
String

the string to be parsed.

Returns

a Float object holding the value represented by the String argument.

Attributes

Exceptions

if string can not be parsed as a float value.

Remarks

Returns 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. Whitespace is removed as if by the String#trim method; that is, both ASCII space and control characters are removed. The rest of s should constitute a FloatValue as described by the lexical syntax rules:

<blockquote> <dl> <dt>FloatValue:<dd>Sign<sub>opt</sub>NaN<dd>Sign<sub>opt</sub>Infinity<dd>Sign<sub>opt</sub> FloatingPointLiteral<dd>Sign<sub>opt</sub> HexFloatingPointLiteral<dd>SignedInteger</dl>

<dl> <dt>HexFloatingPointLiteral: <dd> HexSignificand BinaryExponent FloatTypeSuffix<sub>opt</sub></dl>

<dl> <dt>HexSignificand:<dd>HexNumeral<dd>HexNumeral.<dd>0xHexDigits<sub>opt</sub> . HexDigits<dd>0X HexDigits<sub>opt</sub> .HexDigits</dl>

<dl> <dt>BinaryExponent:<dd>BinaryExponentIndicator SignedInteger</dl>

<dl> <dt>BinaryExponentIndicator:<dd>p<dd>P</dl>

</blockquote>

where Sign, FloatingPointLiteral, HexNumeral, HexDigits, SignedInteger and FloatTypeSuffix are as defined in the lexical structure sections of <cite>The Java Language Specification</cite>, except that underscores are not accepted between digits. 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" or as an exact hexadecimal value; this exact numerical 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.

Note that the round-to-nearest rule also implies overflow and underflow behaviour; if the exact value of s is large enough in magnitude (greater than or equal to (#MAX_VALUE + Math#ulp(float) ulp(MAX_VALUE)/2), rounding to float will result in an infinity and if the exact value of s is small enough in magnitude (less than or equal to #MIN_VALUE/2), rounding to float will result in a zero.

Finally, after rounding a Float object representing this float value is returned.

To interpret localized string representations of a floating-point value, use subclasses of java.text.NumberFormat.

Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do <em>not</em> influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type. In general, the two-step sequence of conversions, string to double followed by double to float, is <em>not</em> equivalent to converting a string directly to float. For example, if first converted to an intermediate double and then to float, the string<br> "1.00000017881393421514957253748434595763683319091796875001d"<br> results in the float value 1.0000002f; if the string is converted directly to float, 1.000000<b>1</b>f results.

To avoid calling this method on an invalid string and having a NumberFormatException be thrown, the documentation for Double#valueOf Double.valueOf lists a regular expression which can be used to screen the input.

Java documentation for java.lang.Float.valueOf(java.lang.String).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

See also

Applies to