public final class Double
extends Number implements Comparable<Double>
| java.lang.Object | ||
| java.lang.Number | ||
| java.lang.Double | ||
Double类将原始类型double的值包装在对象中。 类型为Double的对象包含类型为double的单个字段。
另外,该类提供了几种将 double转换为 String和 String转换为 double ,以及处理 double时有用的其他常量和方法。
常量(Constants) |
|
|---|---|
int |
BYTES 用于表示 |
int |
MAX_EXPONENT 变量可能具有的最大指数有限 |
double |
MAX_VALUE 的常量保持型的最大正的有限值 |
int |
MIN_EXPONENT 一个归一化的 |
double |
MIN_NORMAL 常数保持 |
double |
MIN_VALUE 的常量保持类型的最小正非零值 |
double |
NEGATIVE_INFINITY 持有类型为 |
double |
NaN 一个常量,其中 |
double |
POSITIVE_INFINITY 保持 |
int |
SIZE 用于表示 |
Fields |
|
|---|---|
public static final Class<Double> |
TYPE 代表原始类型 |
Public constructors |
|
|---|---|
Double(double value) 构造一个新分配的 |
|
Double(String s) 构造一个新分配的 |
|
公共方法(Public methods) |
|
|---|---|
byte |
byteValue() 将此 |
static int |
compare(double d1, double d2) 比较两个指定的 |
int |
compareTo(Double anotherDouble) 以数字方式比较两个 |
static long |
doubleToLongBits(double value) 根据IEEE 754浮点“双格式”位布局返回指定浮点值的表示形式。 |
static long |
doubleToRawLongBits(double value) 根据IEEE 754浮点“双格式”位布局返回指定浮点值的表示形式,保留非数字(NaN)值。 |
double |
doubleValue() 返回此 |
boolean |
equals(Object obj) 将此对象与指定的对象进行比较。 |
float |
floatValue() 返回此 |
int |
hashCode() 返回此 |
static int |
hashCode(double value) 返回 |
int |
intValue() 返回此的值 |
static boolean |
isFinite(double d) 如果参数是有限浮点值,则返回 |
boolean |
isInfinite() 返回 |
static boolean |
isInfinite(double v) 返回 |
static boolean |
isNaN(double v) 返回 |
boolean |
isNaN() 返回 |
static double |
longBitsToDouble(long bits) 返回对应于给定位表示的 |
long |
longValue() 返回此的值 |
static double |
max(double a, double b) 通过调用 |
static double |
min(double a, double b) |
static double |
parseDouble(String s) 返回一个新的 |
short |
shortValue() 将此 |
static double |
sum(double a, double b) 根据+运算符将两个 |
static String |
toHexString(double d) 返回 |
static String |
toString(double d) 返回 |
String |
toString() 返回此 |
static Double |
valueOf(String s) 返回保存由参数字符串 |
static Double |
valueOf(double d) 返回表示指定的 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Number
|
|
java.lang.Object
|
|
java.lang.Comparable
|
|
int MAX_EXPONENT
变量可能有的最大指数是有限的double 。 它等于由Math.getExponent(Double.MAX_VALUE)返回的值。
常量值:1023(0x000003ff)
double MAX_VALUE
的常量保持型的最大正的有限值double ,(2-2 -52)A·2 1023。 它等于十六进制浮点0x1.fffffffffffffP+1023 ,也等于Double.longBitsToDouble(0x7fefffffffffffffL) 。
常数值:1.7976931348623157E308
int MIN_EXPONENT
归一化的double变量可能具有的最小指数。 它等于Math.getExponent(Double.MIN_NORMAL)返回的值。
常量值:-1022(0xfffffc02)
double MIN_NORMAL
一个常量保持double -1022型的最小正常正常值。 它等于十六进制浮点0x1.0p-1022 ,也等于Double.longBitsToDouble(0x0010000000000000L) 。
常数值:2.2250738585072014E-308
double MIN_VALUE
的常量保持类型的最小正非零值double ,2 -1074。 它等于十六进制浮点0x0.0000000000001P-1022 ,也等于Double.longBitsToDouble(0x1L) 。
常数值:4.9E-324
double NEGATIVE_INFINITY
类型为double的负无穷大的double 。 它等于Double.longBitsToDouble(0xfff0000000000000L)返回的值。
常数值:-Infinity
double NaN
一个常量包含类型为double非数字(NaN)值。 它相当于Double.longBitsToDouble(0x7ff8000000000000L)返回的值。
常数值:NaN
double POSITIVE_INFINITY
保持double类型的正无穷大的double 。 它等于Double.longBitsToDouble(0x7ff0000000000000L)返回的值。
恒定值:无限
Double (double value)
构造一个新分配 Double对象表示基本 double说法。
| 参数(Parameters) | |
|---|---|
value |
double: the value to be represented by the Double. |
Double (String s)
构造一个新分配的Double对象,该对象表示由字符串表示的类型为double的浮点值。 字符串被转换为double值,就像通过valueOf方法一样。
| 参数(Parameters) | |
|---|---|
s |
String: a string to be converted to a Double. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the string does not contain a parsable number. |
也可以看看:
byte byteValue ()
将此 Double的值作为 byte (通过转换为 byte )。
| 返回(Returns) | |
|---|---|
byte |
the double value represented by this object converted to type byte |
int compare (double d1,
double d2)
比较两个指定的double值。 返回的整数值的符号与调用返回的整数的符号相同:
new Double(d1).compareTo(new Double(d2))
| 参数(Parameters) | |
|---|---|
d1 |
double: the first double to compare |
d2 |
double: the second double to compare |
| 返回(Returns) | |
|---|---|
int |
the value 0 if d1 is numerically equal to d2; a value less than 0 if d1 is numerically less than d2; and a value greater than 0 if d1 is numerically greater than d2. |
int compareTo (Double anotherDouble)
以数字方式比较两个Double对象。 有两种方法可以使用此方法执行的比较与Java语言数值比较运算符( <, <=, ==, >=, > )在应用于基本double值时执行的比较有所不同:
Double.NaN is considered by this method to be equal to itself and greater than all other double values (including Double.POSITIVE_INFINITY). 0.0d is considered by this method to be greater than -0.0d. Double objects imposed by this method is
consistent with equals.
| 参数(Parameters) | |
|---|---|
anotherDouble |
Double: the Double to be compared. |
| 返回(Returns) | |
|---|---|
int |
the value 0 if anotherDouble is numerically equal to this Double; a value less than 0 if this Double is numerically less than anotherDouble; and a value greater than 0 if this Double is numerically greater than anotherDouble. |
long doubleToLongBits (double value)
根据IEEE 754浮点“双格式”位布局返回指定浮点值的表示形式。
位63(由掩码0x8000000000000000L选择的位)表示浮点数的符号。 位62-52(由掩码0x7ff0000000000000L选择的位)表示指数。 位51-0(由掩码0x000fffffffffffffL选择的位)表示0x000fffffffffffffL的有效位(有时称为尾数)。
如果参数是正无穷大,结果是 0x7ff0000000000000L 。
如果参数是负无穷大,则结果为 0xfff0000000000000L 。
如果参数是NaN,那么结果是 0x7ff8000000000000L 。
在所有情况下,结果都是一个 long整数,当给予 longBitsToDouble(long)方法时,将产生一个与 doubleToLongBits的参数相同的浮点值(除了所有NaN值都折叠为单个“规范”NaN值)。
| 参数(Parameters) | |
|---|---|
value |
double: a double precision floating-point number. |
| 返回(Returns) | |
|---|---|
long |
the bits that represent the floating-point number. |
long doubleToRawLongBits (double value)
根据IEEE 754浮点“双格式”位布局返回指定浮点值的表示形式,保留非数字(NaN)值。
位63(由掩码0x8000000000000000L选择的位)表示浮点数的符号。 位62-52(由掩码0x7ff0000000000000L选择的位)表示指数。 位51-0(由掩码0x000fffffffffffffL选择的位)表示0x000fffffffffffffL的有效位(有时称为尾数)。
如果参数是正无穷大,结果是 0x7ff0000000000000L 。
如果参数是负无穷大,结果是 0xfff0000000000000L 。
如果参数是NaN,则结果是代表实际NaN值的long整数。 与doubleToLongBits方法不同, doubleToRawLongBits不会将编码NaN的所有位模式折叠为单个“规范”NaN值。
在所有情况下,结果都是一个 long整数,当给予 longBitsToDouble(long)方法时,将产生一个与 doubleToRawLongBits的参数相同的浮点值。
| 参数(Parameters) | |
|---|---|
value |
double: a double precision floating-point number. |
| 返回(Returns) | |
|---|---|
long |
the bits that represent the floating-point number. |
double doubleValue ()
返回此 Double对象的 double值。
| 返回(Returns) | |
|---|---|
double |
the double value represented by this object |
boolean equals (Object obj)
将此对象与指定的对象进行比较。 其结果是true当且仅当该参数不是null并且是Double对象,它表示一个double具有相同的值作为double该对象表示。 为此,当且仅当方法doubleToLongBits(double)在应用于每个值时返回相同的long值时,两个double值double视为相同。
请注意,在大多数情况下,类的两个实例 Double , d1和 d2 ,价值 d1.equals(d2)为 true当且仅当
d1.doubleValue() == d2.doubleValue()
也有值true 。 但是,有两个例外:
d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false. d1 represents +0.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true. | 参数(Parameters) | |
|---|---|
obj |
Object: the object to compare with. |
| 返回(Returns) | |
|---|---|
boolean |
true if the objects are the same; false otherwise. |
也可以看看:
float floatValue ()
返回此 Double对象的 float值。
| 返回(Returns) | |
|---|---|
float |
the double value represented by this object converted to type float |
int hashCode ()
返回此Double对象的哈希码。 结果是long整数位表示的两个半部分(与由方法doubleToLongBits(double)产生的完全相同)由此Double对象表示的基本double值的两半的异或结果。 也就是说,哈希码是表达式的值:
(int)(v^(v>>>32))
where
v is defined by:
long v = Double.doubleToLongBits(this.doubleValue());
| 返回(Returns) | |
|---|---|
int |
a hash code value for this object. |
int hashCode (double value)
返回double值的哈希码; 与Double.hashCode()兼容。
| 参数(Parameters) | |
|---|---|
value |
double: the value to hash |
| 返回(Returns) | |
|---|---|
int |
a hash code value for a double value. |
int intValue ()
返回此的值 Double作为 int (通过转换成键入 int )。
| 返回(Returns) | |
|---|---|
int |
the double value represented by this object converted to type int |
boolean isFinite (double d)
如果参数是有限浮点值,则返回true ; 否则返回false (对于NaN和无穷大参数)。
| 参数(Parameters) | |
|---|---|
d |
double: the double value to be tested |
| 返回(Returns) | |
|---|---|
boolean |
true if the argument is a finite floating-point value, false otherwise. |
boolean isInfinite ()
返回 true如果此 Double值是无限大, false否则。
| 返回(Returns) | |
|---|---|
boolean |
true if the value represented by this object is positive infinity or negative infinity; false otherwise. |
boolean isInfinite (double v)
返回 true如果指定的数是无限大, false否则。
| 参数(Parameters) | |
|---|---|
v |
double: the value to be tested. |
| 返回(Returns) | |
|---|---|
boolean |
true if the value of the argument is positive infinity or negative infinity; false otherwise. |
boolean isNaN (double v)
返回 true如果指定的号码是一个不一个数字(NaN)值, false否则。
| 参数(Parameters) | |
|---|---|
v |
double: the value to be tested. |
| 返回(Returns) | |
|---|---|
boolean |
true if the value of the argument is NaN; false otherwise. |
boolean isNaN ()
返回 true如果此 Double值是不是非数字(NAN), false否则。
| 返回(Returns) | |
|---|---|
boolean |
true if the value represented by this object is NaN; false otherwise. |
double longBitsToDouble (long bits)
返回对应于给定位表示的double值。 该参数被认为是根据IEEE 754浮点“双格式”位布局的浮点值的表示。
如果参数是 0x7ff0000000000000L ,结果是正无穷。
如果参数是 0xfff0000000000000L ,结果是负无穷大。
如果参数在上述范围内的任何值0x7ff0000000000001L通过0x7fffffffffffffffL或在范围0xfff0000000000001L通过0xffffffffffffffffL ,结果是NaN。 Java提供的IEEE 754浮点操作不能区分具有不同位模式的相同类型的两个NaN值。 NaN的不同值只能通过使用Double.doubleToRawLongBits方法区分。
在所有其他情况下,让 s , e和 m为可以从参数计算的三个值:
int s = ((bits >> 63) == 0) ? 1 : -1;
int e = (int)((bits >> 52) & 0x7ffL);
long m = (e == 0) ?
(bits & 0xfffffffffffffL) << 1 :
(bits & 0xfffffffffffffL) | 0x10000000000000L;
Then the floating-point result equals the value of the mathematical expression
s·
m·2
e-1075.
请注意,此方法可能无法返回与long参数完全相同的位模式的double NaN。 IEEE 754区分了两种NaN,安静的NaN和信令NaN 。 这两种NaN之间的差异在Java中通常不可见。 信令NaN上的算术运算将它们变成了安静的NaN,它们具有不同但通常相似的位模式。 然而,在一些处理器上,仅仅复制一个信令NaN也会执行该转换。 特别是,复制信号NaN以将其返回给调用方法可执行此转换。 因此longBitsToDouble可能无法以信号NaN位模式返回double 。 因此,对于某些long值, doubleToRawLongBits(longBitsToDouble(start))可能不等于start 。 此外,哪些特定的位模式表示信令NaN是平台相关的; 尽管所有NaN位模式(无声或信令)必须处于上述NaN范围内。
| 参数(Parameters) | |
|---|---|
bits |
long: any long integer. |
| 返回(Returns) | |
|---|---|
double |
the double floating-point value with the same bit pattern. |
long longValue ()
返回此的值 Double作为 long (通过转换成键入 long )。
| 返回(Returns) | |
|---|---|
long |
the double value represented by this object converted to type long |
double max (double a,
double b)
通过调用 Math.max返回两个 double值中较大的 Math.max 。
| 参数(Parameters) | |
|---|---|
a |
double: the first operand |
b |
double: the second operand |
| 返回(Returns) | |
|---|---|
double |
the greater of a and b |
也可以看看:
double min (double a,
double b)
通过调用 Math.min返回两个 double值中较小的 Math.min 。
| 参数(Parameters) | |
|---|---|
a |
double: the first operand |
b |
double: the second operand |
| 返回(Returns) | |
|---|---|
double |
the smaller of a and b. |
也可以看看:
double parseDouble (String s)
返回一个新 double初始化为指定的代表的值 String ,如通过执行 valueOf类的方法 Double 。
| 参数(Parameters) | |
|---|---|
s |
String: the string to be parsed. |
| 返回(Returns) | |
|---|---|
double |
the double value represented by the string argument. |
| 抛出异常(Throws) | |
|---|---|
NullPointerException |
if the string is null |
NumberFormatException |
if the string does not contain a parsable double. |
也可以看看:
short shortValue ()
返回此的值 Double作为 short (通过强制转换为 short )。
| 返回(Returns) | |
|---|---|
short |
the double value represented by this object converted to type short |
double sum (double a,
double b)
根据+运算符将两个 double值相加。
| 参数(Parameters) | |
|---|---|
a |
double: the first operand |
b |
double: the second operand |
| 返回(Returns) | |
|---|---|
double |
the sum of a and b |
也可以看看:
String toHexString (double d)
返回double参数的十六进制字符串表示形式。 下面提到的所有字符都是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". double 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. double 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-1022". 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 |
Double.MAX_VALUE |
0x1.fffffffffffffp1023 |
Minimum Normal Value |
0x1.0p-1022 |
Maximum Subnormal Value |
0x0.fffffffffffffp-1022 |
Double.MIN_VALUE |
0x0.0000000000001p-1022 |
| 参数(Parameters) | |
|---|---|
d |
double: the double to be converted. |
| 返回(Returns) | |
|---|---|
String |
a hex string representation of the argument. |
String toString (double d)
返回double参数的字符串表示形式。 下面提到的所有字符都是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). double. That is, suppose that
x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument
d. Then
d must be the
double value nearest to
x; or if two
double values are equally close to
x, then
d must be one of them and the least significant bit of the significand of
d must be
0.
要创建浮点值的本地化字符串表示,请使用 NumberFormat子类。
| 参数(Parameters) | |
|---|---|
d |
double: the double to be converted. |
| 返回(Returns) | |
|---|---|
String |
a string representation of the argument. |
String toString ()
返回此Double对象的字符串表示形式。 此对象表示的原语double值完全转换为字符串,就像通过一个参数的方法toString 。
| 返回(Returns) | |
|---|---|
String |
a String representation of this object. |
也可以看看:
Double valueOf (String s)
返回保存由参数字符串 s表示的 double值的 Double对象。
如果 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
double 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
double 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
Double object representing this
double value is returned.
要解释浮点值的本地化字符串表示,请使用 NumberFormat子类。
请注意,尾部格式说明符,确定浮点文字类型的说明符( 1.0f是float值; 1.0d是double值), 不会影响此方法的结果。 换句话说,输入字符串的数值直接转换为目标浮点类型。 两步转换序列(字符串为float后跟float至double ) 不等同于将字符串直接转换为double 。 例如, float文字0.1f等于double值0.10000000149011612 ; 所述float字面0.1f表示不同的数值比double字面0.1 。 (数值0.1不能用二进制浮点数精确表示。)
为避免在无效字符串上调用此方法并引发 NumberFormatException ,可使用下面的正则表达式来筛选输入字符串:
final String Digits = "(\\p{Digit}+)";
final String HexDigits = "(\\p{XDigit}+)";
// an exponent is 'e' or 'E' followed by an optionally
// signed decimal integer.
final String Exp = "[eE][+-]?"+Digits;
final String fpRegex =
("[\\x00-\\x20]*"+ // Optional leading "whitespace"
"[+-]?(" + // Optional sign character
"NaN|" + // "NaN" string
"Infinity|" + // "Infinity" string
// A decimal floating-point string representing a finite positive
// number without a leading sign has at most five basic pieces:
// Digits . Digits ExponentPart FloatTypeSuffix
//
// Since this method allows integer-only strings as input
// in addition to strings of floating-point literals, the
// two sub-patterns below are simplifications of the grammar
// productions from section 3.10.2 of
// The JavaTM Language Specification.
// Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
"((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
// . Digits ExponentPart_opt FloatTypeSuffix_opt
"(\\.("+Digits+")("+Exp+")?)|"+
// Hexadecimal strings
"((" +
// 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "(\\.)?)|" +
// 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
"(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
")[pP][+-]?" + Digits + "))" +
"[fFdD]?))" +
"[\\x00-\\x20]*");// Optional trailing "whitespace"
if (Pattern.matches(fpRegex, myString))
Double.valueOf(myString); // Will not throw NumberFormatException
else {
// Perform suitable alternative action
}
| 参数(Parameters) | |
|---|---|
s |
String: the string to be parsed. |
| 返回(Returns) | |
|---|---|
Double |
a Double object holding the value represented by the String argument. |
| 抛出异常(Throws) | |
|---|---|
NumberFormatException |
if the string does not contain a parsable number. |
Double valueOf (double d)
返回表示指定的double值的Double实例。 如果不需要新的Double实例,则通常应该优先使用此方法,而不是构造函数Double(double) ,因为此方法可能通过缓存经常请求的值来产生显着更好的空间和时间性能。
| 参数(Parameters) | |
|---|---|
d |
double: a double value. |
| 返回(Returns) | |
|---|---|
Double |
a Double instance representing d. |