public final class Float
extends Number implements Comparable<Float>
| java.lang.Object | ||
| java.lang.Number | ||
| java.lang.Float | ||
Float类将原始类型float的值包装在对象中。 类型为Float的对象包含一个类型为float字段。
此外,该类提供了几种将 float转换为 String和 String转换为 float ,以及处理 float时有用的其他常量和方法。
常量(Constants) |
|
|---|---|
int |
BYTES 用于表示 |
int |
MAX_EXPONENT 变量可能具有的最大指数有限 |
float |
MAX_VALUE 一个常数保持类型 |
int |
MIN_EXPONENT 标准化的 |
float |
MIN_NORMAL 一个常量保持 |
float |
MIN_VALUE 一个常量保持 |
float |
NEGATIVE_INFINITY 类型为 |
float |
NaN 一个常量,其中 |
float |
POSITIVE_INFINITY 一个常量保持 |
int |
SIZE 用于表示 |
Fields |
|
|---|---|
public static final Class<Float> |
TYPE 表示原始类型 |
Public constructors |
|
|---|---|
Float(float value) 构造一个新分配的 |
|
Float(double value) 构造一个新分配的 |
|
Float(String s) 构造一个新分配的 |
|
公共方法(Public methods) |
|
|---|---|
byte |
byteValue() 将此 |
static int |
compare(float f1, float f2) 比较两个指定的 |
int |
compareTo(Float anotherFloat) 数字比较两个 |
double |
doubleValue() 返回此 |
boolean |
equals(Object obj) 将此对象与指定的对象进行比较。 |
static int |
floatToIntBits(float value) 根据IEEE 754浮点“单一格式”位布局返回指定浮点值的表示形式。 |
static int |
floatToRawIntBits(float value) 根据IEEE 754浮点“单一格式”位布局返回指定浮点值的表示形式,保留非数字(NaN)值。 |
float |
floatValue() 返回此 |
int |
hashCode() 返回此 |
static int |
hashCode(float value) 返回 |
static float |
intBitsToFloat(int bits) 返回对应于给定位表示的 |
int |
intValue() 返回此的值 |
static boolean |
isFinite(float f) 如果参数是有限浮点值,则返回 |
boolean |
isInfinite() 返回 |
static boolean |
isInfinite(float v) 返回 |
static boolean |
isNaN(float v) 返回 |
boolean |
isNaN() 返回 |
long |
longValue() 将此 |
static float |
max(float a, float b) 通过调用 |
static float |
min(float a, float b) |
static float |
parseFloat(String s) 返回一个新 |
short |
shortValue() 将此 |
static float |
sum(float a, float b) 根据+运算符将两个 |
static String |
toHexString(float f) 返回 |
String |
toString() 返回此 |
static String |
toString(float f) 返回 |
static Float |
valueOf(String s) 返回保存由参数字符串 |
static Float |
valueOf(float f) 返回表示指定的 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Number
|
|
java.lang.Object
|
|
java.lang.Comparable
|
|
int MAX_EXPONENT
变量可能具有的最大指数是有限的float 。 它等于由Math.getExponent(Float.MAX_VALUE)返回的值。
常量值:127(0x0000007f)
float MAX_VALUE
常数保持类型float ,(2-2 -23 )·2 127的最大正有限值。 它等于十六进制浮点0x1.fffffeP+127f ,也等于Float.intBitsToFloat(0x7f7fffff) 。
常数值:3.4028235E38
int MIN_EXPONENT
标准化的float变量可能具有的最小指数。 它等于由Math.getExponent(Float.MIN_NORMAL)返回的值。
常量值:-126(0xffffff82)
float MIN_NORMAL
一个常量保持float -126型的最小正常正常值。 它等于十六进制浮点0x1.0p-126f ,也等于Float.intBitsToFloat(0x00800000) 。
常数值:1.17549435E-38
float MIN_VALUE
一个常量保持float -149型的最小正非零值。 它等于十六进制浮点0x0.000002P-126f ,也等于Float.intBitsToFloat(0x1) 。
常数值:1.4E-45
float NEGATIVE_INFINITY
持有float类型的负无穷大的float 。 它等于Float.intBitsToFloat(0xff800000)返回的值。
常数值:-Infinity
float NaN
一个常量包含float类型的非数字(NaN)值。 它等于由Float.intBitsToFloat(0x7fc00000)返回的值。
常数值:NaN
float POSITIVE_INFINITY
一个常量,保持类型为float的正无穷大。 它等于Float.intBitsToFloat(0x7f800000)返回的值。
恒定值:无限
Float (float value)
构造一个新分配的 Float对象,该对象表示原始参数 float 。
| 参数(Parameters) | |
|---|---|
value |
float: the value to be represented by the Float. |
Float (double value)
构造一个新分配的 Float对象,该对象表示转换为类型 float的参数。
| 参数(Parameters) | |
|---|---|
value |
double: the value to be represented by the Float. |
Float (String s)
构造一个新分配的Float对象,该对象表示由字符串表示的类型为float的浮点值。 该字符串被转换为float值,就像通过valueOf方法一样。
| 参数(Parameters) | |
|---|---|
s |
String: a string to be converted to a Float. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the string does not contain a parsable number. |
也可以看看:
byte byteValue ()
将此 Float的值作为 byte (通过转换为 byte )。
| 返回(Returns) | |
|---|---|
byte |
the float value represented by this object converted to type byte |
int compare (float f1,
float f2)
比较两个指定的float值。 返回的整数值的符号与调用返回的整数的符号相同:
new Float(f1).compareTo(new Float(f2))
| 参数(Parameters) | |
|---|---|
f1 |
float: the first float to compare. |
f2 |
float: the second float to compare. |
| 返回(Returns) | |
|---|---|
int |
the value 0 if f1 is numerically equal to f2; a value less than 0 if f1 is numerically less than f2; and a value greater than 0 if f1 is numerically greater than f2. |
int compareTo (Float anotherFloat)
以数字方式比较两个Float对象。 有两种方法可以使用此方法执行的比较与Java语言数值比较运算符( <, <=, ==, >=, > )在应用于基本float值时执行的比较有所不同:
Float.NaN is considered by this method to be equal to itself and greater than all other float values (including Float.POSITIVE_INFINITY). 0.0f is considered by this method to be greater than -0.0f. Float objects imposed by this method is
consistent with equals.
| 参数(Parameters) | |
|---|---|
anotherFloat |
Float: the Float to be compared. |
| 返回(Returns) | |
|---|---|
int |
the value 0 if anotherFloat is numerically equal to this Float; a value less than 0 if this Float is numerically less than anotherFloat; and a value greater than 0 if this Float is numerically greater than anotherFloat. |
也可以看看:
double doubleValue ()
返回此 Float对象的 double值。
| 返回(Returns) | |
|---|---|
double |
the float value represented by this object is converted to type double and the result of the conversion is returned. |
boolean equals (Object obj)
将此对象与指定的对象进行比较。 其结果是true当且仅当该参数不是null并且是Float对象,它表示一个float具有相同的值作为float该对象表示。 为此,当且仅当方法floatToIntBits(float)在应用于每个值时返回相同的int值时,两个float值float视为相同。
请注意,在大多数情况下,类的两个实例 Float , f1和 f2 ,价值 f1.equals(f2)为 true当且仅当
f1.floatValue() == f2.floatValue()
也有值true 。 但是,有两个例外:
f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false. f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true. | 参数(Parameters) | |
|---|---|
obj |
Object: the object to be compared |
| 返回(Returns) | |
|---|---|
boolean |
true if the objects are the same; false otherwise. |
也可以看看:
int floatToIntBits (float value)
根据IEEE 754浮点“单一格式”位布局返回指定浮点值的表示形式。
位31(由掩码0x80000000选择的位)表示浮点数的符号。 位30-23(由掩码0x7f800000选择的位)表示指数。 位22-0(由掩码0x007fffff选择的位)表示0x007fffff的有效位(有时称为尾数)。
如果参数是正无穷大,则结果是 0x7f800000 。
如果参数是负无穷大,结果是 0xff800000 。
如果参数是NaN,那么结果是 0x7fc00000 。
在所有情况下,结果都是一个整数,当给予 intBitsToFloat(int)方法时,将产生一个与 floatToIntBits的参数相同的浮点值(除了所有NaN值被折叠为单个“规范”NaN值)。
| 参数(Parameters) | |
|---|---|
value |
float: a floating-point number. |
| 返回(Returns) | |
|---|---|
int |
the bits that represent the floating-point number. |
int floatToRawIntBits (float value)
根据IEEE 754浮点“单一格式”位布局返回指定浮点值的表示形式,保留非数字(NaN)值。
位31(由掩码0x80000000选择的位)表示浮点数的符号。 位30-23(由掩码0x7f800000选择的位)表示指数。 位22-0(由掩码0x007fffff选择的位)表示0x007fffff的有效位(有时称为尾数)。
如果参数是正无穷大,则结果为 0x7f800000 。
如果参数是负无穷大,则结果为 0xff800000 。
如果参数是NaN,则结果是表示实际NaN值的整数。 与floatToIntBits方法不同, floatToRawIntBits不会将编码NaN的所有位模式折叠为单个“规范”NaN值。
在所有情况下,结果都是一个整数,当给予 intBitsToFloat(int)方法时,将产生一个与 floatToRawIntBits的参数相同的浮点值。
| 参数(Parameters) | |
|---|---|
value |
float: a floating-point number. |
| 返回(Returns) | |
|---|---|
int |
the bits that represent the floating-point number. |
float floatValue ()
返回此 Float对象的 float值。
| 返回(Returns) | |
|---|---|
float |
the float value represented by this object |
int hashCode ()
返回此Float对象的哈希码。 结果是由该Float对象所表示的原语float值的整数位表示,完全如由方法floatToIntBits(float)所产生的。
| 返回(Returns) | |
|---|---|
int |
a hash code value for this object. |
int hashCode (float value)
返回float值的散列码; 与Float.hashCode()兼容。
| 参数(Parameters) | |
|---|---|
value |
float: the value to hash |
| 返回(Returns) | |
|---|---|
int |
a hash code value for a float value. |
float intBitsToFloat (int bits)
返回对应于给定位表示的float值。 该参数被认为是根据IEEE 754浮点“单格式”位布局的浮点值的表示。
如果参数是 0x7f800000 ,结果是正无穷。
如果参数是 0xff800000 ,结果是负无穷。
如果参数在上述范围内的任何值0x7f800001通过0x7fffffff或在范围0xff800001通过0xffffffff ,结果是NaN。 Java提供的IEEE 754浮点操作不能区分具有不同位模式的相同类型的两个NaN值。 NaN的不同值只能通过使用Float.floatToRawIntBits方法区分。
在所有其他情况下,让 s , e和 m为可以从参数计算的三个值:
int s = ((bits >> 31) == 0) ? 1 : -1;
int e = ((bits >> 23) & 0xff);
int m = (e == 0) ?
(bits & 0x7fffff) << 1 :
(bits & 0x7fffff) | 0x800000;
Then the floating-point result equals the value of the mathematical expression
s·
m·2
e-150.
请注意,此方法可能无法返回与int参数完全相同的位模式的float NaN。 IEEE 754区分了两种NaN,安静的NaN和信令NaN 。 这两种NaN之间的差异在Java中通常不可见。 信令NaN上的算术运算将它们变成了安静的NaN,它们具有不同但通常相似的位模式。 然而,在一些处理器上,仅仅复制一个信令NaN也会执行该转换。 特别是,复制信号NaN以将其返回给调用方法可执行此转换。 因此intBitsToFloat可能无法以信号NaN位模式返回float 。 因此,对于某些int值, floatToRawIntBits(intBitsToFloat(start))可能不等于start 。 此外,哪些特定的位模式表示信令NaN是平台相关的; 尽管所有NaN位模式(无声或信令)必须处于上述NaN范围内。
| 参数(Parameters) | |
|---|---|
bits |
int: an integer. |
| 返回(Returns) | |
|---|---|
float |
the float floating-point value with the same bit pattern. |
int intValue ()
返回此的值 Float作为 int (通过转换成键入 int )。
| 返回(Returns) | |
|---|---|
int |
the float value represented by this object converted to type int |
boolean isFinite (float f)
如果参数是有限浮点值,则返回true ; 否则返回false (对于NaN和无穷大参数)。
| 参数(Parameters) | |
|---|---|
f |
float: the float value to be tested |
| 返回(Returns) | |
|---|---|
boolean |
true if the argument is a finite floating-point value, false otherwise. |
boolean isInfinite ()
返回 true如果此 Float值是无限大, false否则。
| 返回(Returns) | |
|---|---|
boolean |
true if the value represented by this object is positive infinity or negative infinity; false otherwise. |
boolean isInfinite (float v)
返回 true如果指定的数是无限大, false否则。
| 参数(Parameters) | |
|---|---|
v |
float: the value to be tested. |
| 返回(Returns) | |
|---|---|
boolean |
true if the argument is positive infinity or negative infinity; false otherwise. |
boolean isNaN (float v)
返回 true如果指定的号码是一个不一个数字(NaN)值, false否则。
| 参数(Parameters) | |
|---|---|
v |
float: the value to be tested. |
| 返回(Returns) | |
|---|---|
boolean |
true if the argument is NaN; false otherwise. |
boolean isNaN ()
返回 true如果此 Float值是不是非数字(NAN), false否则。
| 返回(Returns) | |
|---|---|
boolean |
true if the value represented by this object is NaN; false otherwise. |
long longValue ()
将此 Float值作为 long返回(通过强制转换为键入 long )。
| 返回(Returns) | |
|---|---|
long |
the float value represented by this object converted to type long |
float max (float a,
float b)
通过调用 Math.max返回两个 float值中的较大值。
| 参数(Parameters) | |
|---|---|
a |
float: the first operand |
b |
float: the second operand |
| 返回(Returns) | |
|---|---|
float |
the greater of a and b |
也可以看看:
float min (float a,
float b)
通过调用 Math.min返回两个 float值中较小的 Math.min 。
| 参数(Parameters) | |
|---|---|
a |
float: the first operand |
b |
float: the second operand |
| 返回(Returns) | |
|---|---|
float |
the smaller of a and b |
也可以看看:
float parseFloat (String s)
返回一个新的 float初始化为指定的 String表示的值,如 valueOf类的方法 String所执行的 Float 。
| 参数(Parameters) | |
|---|---|
s |
String: the string to be parsed. |
| 返回(Returns) | |
|---|---|
float |
the float value represented by the string argument. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the string is null |
NumberFormatException |
if the string does not contain a parsable float. |
也可以看看:
short shortValue ()
将此 Float的值作为 short (通过转换为 short )。
| 返回(Returns) | |
|---|---|
short |
the float value represented by this object converted to type short |
float sum (float a,
float b)
根据+运算符将两个 float值相加。
| 参数(Parameters) | |
|---|---|
a |
float: the first operand |
b |
float: the second operand |
| 返回(Returns) | |
|---|---|
float |
the sum of a and b |
也可以看看:
String toHexString (float f)
返回float参数的十六进制字符串表示形式。 下面提到的所有字符都是ASCII字符。
NaN". -' ('\u002D'); if the sign is positive, no sign character appears in the result. As for the magnitude m:
"Infinity"; thus, positive infinity produces the result "Infinity" and negative infinity produces the result "-Infinity". "0x0.0p0"; thus, negative zero produces the result "-0x0.0p0" and positive zero produces the result "0x0.0p0". float value with a normalized representation, substrings are used to represent the significand and exponent fields. The significand is represented by the characters "0x1." followed by a lowercase hexadecimal representation of the rest of the significand as a fraction. Trailing zeros in the hexadecimal representation are removed unless all the digits are zero, in which case a single zero is used. Next, the exponent is represented by "p" followed by a decimal string of the unbiased exponent as if produced by a call to Integer.toString on the exponent value. float value with a subnormal representation, the significand is represented by the characters "0x0." followed by a hexadecimal representation of the rest of the significand as a fraction. Trailing zeros in the hexadecimal representation are removed. Next, the exponent is represented by "p-126". Note that there must be at least one nonzero digit in a subnormal significand. | Floating-point Value | Hexadecimal String |
|---|---|
1.0 |
0x1.0p0 |
-1.0 |
-0x1.0p0 |
2.0 |
0x1.0p1 |
3.0 |
0x1.8p1 |
0.5 |
0x1.0p-1 |
0.25 |
0x1.0p-2 |
Float.MAX_VALUE |
0x1.fffffep127 |
Minimum Normal Value |
0x1.0p-126 |
Maximum Subnormal Value |
0x0.fffffep-126 |
Float.MIN_VALUE |
0x0.000002p-126 |
| 参数(Parameters) | |
|---|---|
f |
float: the float to be converted. |
| 返回(Returns) | |
|---|---|
String |
a hex string representation of the argument. |
String toString ()
返回此Float对象的字符串表示形式。 该对象表示的原始float值将转换为String ,就好像通过一个参数的方法toString 。
| 返回(Returns) | |
|---|---|
String |
a String representation of this object. |
也可以看看:
String toString (float f)
返回float参数的字符串表示形式。 下面提到的所有字符都是ASCII字符。
NaN". -' ('\u002D'); if the sign is positive, no sign character appears in the result. As for the magnitude m:
"Infinity"; thus, positive infinity produces the result "Infinity" and negative infinity produces the result "-Infinity". "0.0"; thus, negative zero produces the result "-0.0" and positive zero produces the result "0.0". .' ('\u002E'), followed by one or more decimal digits representing the fractional part of m. .' ('\u002E'), followed by decimal digits representing the fractional part of a, followed by the letter 'E' ('\u0045'), followed by a representation of n as a decimal integer, as produced by the method toString(int). float. That is, suppose that
x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument
f. Then
f must be the
float value nearest to
x; or, if two
float values are equally close to
x, then
f must be one of them and the least significant bit of the significand of
f must be
0.
要创建浮点值的本地化字符串表示,请使用 NumberFormat子类。
| 参数(Parameters) | |
|---|---|
f |
float: the float to be converted. |
| 返回(Returns) | |
|---|---|
String |
a string representation of the argument. |
Float valueOf (String s)
返回保存由参数字符串 s表示的 float值的 Float对象。
如果 s是 null ,则引发 NullPointerException 。
忽略s中的前导和尾随空白字符。 按照trim()方法删除空格; 也就是说,ASCII空间和控制字符都被删除。 s的其余部分应构成一个FloatValue ,如词法语法规则所述:
where Sign, FloatingPointLiteral, HexNumeral, HexDigits, SignedInteger and FloatTypeSuffix are as defined in the lexical structure sections of The JavaTM Language Specification, except that underscores are not accepted between digits. If
- FloatValue:
- Signopt
NaN- Signopt
Infinity- Signopt FloatingPointLiteral
- Signopt HexFloatingPointLiteral
- SignedInteger
- HexFloatingPointLiteral:
- HexSignificand BinaryExponent FloatTypeSuffixopt
- HexSignificand:
- HexNumeral
- HexNumeral
.0xHexDigitsopt.HexDigits0XHexDigitsopt.HexDigits
- BinaryExponent:
- BinaryExponentIndicator SignedInteger
- BinaryExponentIndicator:
pP
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.
要解释浮点值的本地化字符串表示,请使用 NumberFormat子类。
请注意,尾部格式说明符,用于确定浮点文字类型的说明符( 1.0f是float值; 1.0d是double值), 不会影响此方法的结果。 换句话说,输入字符串的数值直接转换为目标浮点类型。 一般而言,转换的两步骤序列,字符串double随后double到float , 不等同于一个字符串直接转换为float 。 例如,如果首先转换为中间值double ,然后转换为float ,则字符串
"1.00000017881393421514957253748434595763683319091796875001d"
结果float值为1.0000002f ; 如果字符串直接转换为float , 1.0000001f结果为float 。
避免调用上无效字符串这种方法和具有 NumberFormatException被抛出,该文档 Double.valueOf列表正则表达式可用于筛选的输入端。
| 参数(Parameters) | |
|---|---|
s |
String: the string to be parsed. |
| 返回(Returns) | |
|---|---|
Float |
a Float object holding the value represented by the String argument. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the string does not contain a parsable number. |
Float valueOf (float f)
返回表示指定的float值的Float实例。 如果不需要新的Float实例,则通常应优先使用此方法,而不是构造函数Float(float) ,因为此方法通过缓存频繁请求的值可能会产生显着更好的空间和时间性能。
| 参数(Parameters) | |
|---|---|
f |
float: a float value. |
| 返回(Returns) | |
|---|---|
Float |
a Float instance representing f. |