Float (Java Platform SE 8 ) (original) (raw)

Returns a Float object holding thefloat value represented by the argument strings.

If s is null, then aNullPointerException 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:

FloatValue:

Signopt NaN

Signopt Infinity

Signopt FloatingPointLiteral

Signopt HexFloatingPointLiteral

SignedInteger

HexFloatingPointLiteral:

HexSignificand BinaryExponent FloatTypeSuffixopt

HexSignificand:

HexNumeral

HexNumeral .

0x HexDigitsopt . HexDigits

0X HexDigitsopt . HexDigits

BinaryExponent:

BinaryExponentIndicator SignedInteger

BinaryExponentIndicator:

p

P

where Sign, FloatingPointLiteral,HexNumeral, HexDigits, SignedInteger and_FloatTypeSuffix_ are as defined in the lexical structure sections ofThe Java™ Language Specification, 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 + 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 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_not_ 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_not_ equivalent to converting a string directly tofloat. For example, if first converted to an intermediate double and then tofloat, the string
"1.00000017881393421514957253748434595763683319091796875001d"
results in the float value1.0000002f; if the string is converted directly tofloat, 1.000000**1**f results.

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