Double
Double是java中八大基础类型之一double的包装类。
定义
public final class Double extends Number implements Comparable<Double> {}
Double继承自Number,实现了Comparable接口。
属性
//正无穷大,等同于Double.longBitsToDouble(0x7ff0000000000000L)。
public static final double POSITIVE_INFINITY = 1.0 / 0.0;
//负无穷,等同于Double.longBitsToDouble(0xfff0000000000000L)。
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
//非数字(NaN)类型值的常量,等于double.longbitstodouble(0x7ff80000000000000L)
public static final double NaN = 0.0d / 0.0;
//能够表示的最大值只有标准化一种形式 1.7976931348623157e+308
public static final double MAX_VALUE = 0x1.fffffffffffffP+1023;
//能够表示的最小值只有标准化一种形式 2.2250738585072014E-308
public static final double MIN_NORMAL = 0x1.0p-1022;
//指数真值的有效的最大值
public static final int MAX_EXPONENT = 1023;
//指数真值的有效的最小值
public static final int MIN_EXPONENT = -1022;
//用来以二进制补码形式表示 double 值的比特位数
public static final int SIZE = 64;
//1.8新增 用于表示双精度值的字节数
public static final int BYTES = SIZE / Byte.SIZE;
//表示这个类是基础类型double的Class实例
@SuppressWarnings("unchecked")
public static final Class<Double> TYPE = (Class<Double>) Class.getPrimitiveClass("double");
//存储值
private final double value;
构造方法
常用方法
//double 转String
public static String toString(double d) {
return FloatingDecimal.toJavaFormatString(d);
}
//返回Double的十六进制字符串
public static String toHexString(double d) {
/*
* Modeled after the "a" conversion specifier in C99, section
* 7.19.6.1; however, the output of this method is more
* tightly specified.
*/
if (!isFinite(d) )
// For infinity and NaN, use the decimal output.
return Double.toString(d);
else {
// Initialized to maximum size of output.
StringBuilder answer = new StringBuilder(24);
if (Math.copySign(1.0, d) == -1.0) // value is negative,
answer.append("-"); // so append sign info
answer.append("0x");
d = Math.abs(d);
if(d == 0.0) {
answer.append("0.0p0");
} else {
boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
// Isolate significand bits and OR in a high-order bit
// so that the string representation has a known
// length.
long signifBits = (Double.doubleToLongBits(d)
& DoubleConsts.SIGNIF_BIT_MASK) |
0x1000000000000000L;
// Subnormal values have a 0 implicit bit; normal
// values have a 1 implicit bit.
answer.append(subnormal ? "0." : "1.");
// Isolate the low-order 13 digits of the hex
// representation. If all the digits are zero,
// replace with a single 0; otherwise, remove all
// trailing zeros.
String signif = Long.toHexString(signifBits).substring(3,16);
answer.append(signif.equals("0000000000000") ? // 13 zeros
"0":
signif.replaceFirst("0{1,12}$", ""));
answer.append('p');
// If the value is subnormal, use the E_min exponent
// value for double; otherwise, extract and report d's
// exponent (the representation of a subnormal uses
// E_min -1).
answer.append(subnormal ?
DoubleConsts.MIN_EXPONENT:
Math.getExponent(d));
}
return answer.toString();
}
}
//把String对象包装成Double对象
public static Double valueOf(String s) throws NumberFormatException {
return new Double(parseDouble(s));
}
//把基本类型double包装成Double
public static Double valueOf(double d) {
return new Double(d);
}
//String对象转Double
public static double parseDouble(String s) throws NumberFormatException {
return FloatingDecimal.parseDouble(s);
}
//判断是否NaN,是返回true,否则false
public static boolean isNaN(double v) {
return (v != v);
}
//判断是否无穷大
public static boolean isInfinite(double v) {
return (v == POSITIVE_INFINITY) || (v == NEGATIVE_INFINITY);
}
//判断是否有限的浮点数
public static boolean isFinite(double d) {
return Math.abs(d) <= DoubleConsts.MAX_VALUE;
}
//Boolean转String
public String toString() {
return toString(value);
}
//获取哈希码
public static int hashCode(double value) {
long bits = doubleToLongBits(value);
return (int)(bits ^ (bits >>> 32));
}
//重写equals
public boolean equals(Object obj) {
return (obj instanceof Double)
&& (doubleToLongBits(((Double)obj).value) ==
doubleToLongBits(value));
}
//求和 1.8
public static double sum(double a, double b) {
return a + b;
}
//比谁大
public static double max(double a, double b) {
return Math.max(a, b);
}
//比谁小
public static double min(double a, double b) {
return Math.min(a, b);
}