Double
public final class Double extends Number implements Comparable<Double>
继承了Number说明是个数字 ,实现Comparable表可对比
/**
* 表示正无穷
*/
public static final double POSITIVE_INFINITY = 1.0 / 0.0;
/**
* 表示负无穷
*/
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
/**
* 表示非数字NaN
*/
public static final double NaN = 0.0d / 0.0;
/**
* 表示最大正数
*/
public static final double MAX_VALUE = 0x1.fffffffffffffP+1023; // 1.7976931348623157e+308
/**
* 最小标准值
*/
public static final double MIN_NORMAL = 0x1.0p-1022; // 2.2250738585072014E-308
/**
* 最小正数
*/
public static final double MIN_VALUE = 0x0.0000000000001P-1022; // 4.9e-324
/**
* 最大指数
*/
public static final int MAX_EXPONENT = 1023;
/**
* 最小指数
*/
public static final int MIN_EXPONENT = -1022;
/**
* 位数
*/
public static final int SIZE = 64;
/**
* 字节数
*/
public static final int BYTES = SIZE / Byte.SIZE;
指定了正负无穷数,最大最小值等常量
public static String toString(double d) {
return FloatingDecimal.toJavaFormatString(d);
}
调用FloatDecimal类的方法
// 是否超出最大值
public static boolean isFinite(double d) {
return Math.abs(d) <= DoubleConsts.MAX_VALUE;
}
public static String toHexString(double d) {
if (!isFinite(d) )
// For infinity and NaN, use the decimal output.
return Double.toString(d);
else {
// 初始化最大容量24
StringBuilder answer = new StringBuilder(24);
if (Math.copySign(1.0, d) == -1.0) // 判断是否负数
answer.append("-"); // 负数添加负号
answer.append("0x"); // 正数添加0x
d = Math.abs(d);
if(d == 0.0) {
answer.append("0.0p0"); // 为0时返回0.0
} else {
// 判断是否小于最小标准数
boolean subnormal = (d < DoubleConsts.MIN_NORMAL);
// 获取指数位
long signifBits = (Double.doubleToLongBits(d)
& DoubleConsts.SIGNIF_BIT_MASK) |
0x1000000000000000L;
// 小于标准数添加0.,否则1.
answer.append(subnormal ? "0." : "1.");
// 截取返回指定数字signifBits的16进制的无符号整数的字符串
String signif = Long.toHexString(signifBits).substring(3,16);
answer.append(signif.equals("0000000000000") ? // 13 zeros
"0":
signif.replaceFirst("0{1,12}$", ""));
// 添加p
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();
}
}
public static Double valueOf(String s) throws NumberFormatException {
return new Double(parseDouble(s));
}
public static Double valueOf(double d) {
return new Double(d);
}
// 使用FloatDecimal方法
public static double parseDouble(String s) throws NumberFormatException {
return FloatingDecimal.parseDouble(s);
}
// 通过判断非自己的值为NaN
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;
}
private final double value;
public Double(double value) {
this.value = value;
}
public Double(String s) throws NumberFormatException {
value = parseDouble(s);
}
public boolean isNaN() {
return isNaN(value);
}
public boolean isInfinite() {
return isInfinite(value);
}
public String toString() {
return toString(value);
}
public byte byteValue() {
return (byte)value;
}
public short shortValue() {
return (short)value;
}
public int intValue() {
return (int)value;
}
public long longValue() {
return (long)value;
}
public float floatValue() {
return (float)value;
}
public double doubleValue() {
return value;
}
转换byte,short,long,float直接使用强转即可
public int hashCode() {
return Double.hashCode(value);
}
public static int hashCode(double value) {
long bits = doubleToLongBits(value);
return (int)(bits ^ (bits >>> 32));
}
// 判断是否Double类型转为长整形的值是否相等
public boolean equals(Object obj) {
return (obj instanceof Double)
&& (doubleToLongBits(((Double)obj).value) ==
doubleToLongBits(value));
}
// double转长整形
public static long doubleToLongBits(double value) {
long result = doubleToRawLongBits(value);
// Check for NaN based on values of bit fields, maximum
// exponent and nonzero significand.
if ( ((result & DoubleConsts.EXP_BIT_MASK) ==
DoubleConsts.EXP_BIT_MASK) &&
(result & DoubleConsts.SIGNIF_BIT_MASK) != 0L)
result = 0x7ff8000000000000L;
return result;
}
public static native long doubleToRawLongBits(double value);
public static native double longBitsToDouble(long bits);
public int compareTo(Double anotherDouble) {
return Double.compare(value, anotherDouble.value);
}
public static int compare(double d1, double d2) {
if (d1 < d2)
return -1; // 小于返回-1
if (d1 > d2)
return 1; // 大于返回1
// 获取长整值
long thisBits = Double.doubleToLongBits(d1);
long anotherBits = Double.doubleToLongBits(d2);
return (thisBits == anotherBits ? 0 : // Values are equal
(thisBits < anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1)); // (0.0, -0.0) or (NaN, !NaN)
}
// 基础运算
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);
}