比较双值
Compare double values
代码如下:
double d1 = 2.4066419461049408E7;
double d2 = 500.0;
int retval = Double.compare(d1, d2);
if(retval > 0) {
System.out.println("d1 is greater than d2");
} else if(retval < 0) {
System.out.println("d1 is less than d2");
} else {
System.out.println("d1 is equal to d2");
}
输出:
d1 is greater than d2
为什么?如何得到正确答案?
这是正确答案:2.4066419461049408E7
(到目前为止)大于 500.0
注意第一个数字末尾的E7
:这意味着x10^7
(指数)。
您因此将 24066419.461049408
与 500.0
进行比较。
代码如下:
double d1 = 2.4066419461049408E7;
double d2 = 500.0;
int retval = Double.compare(d1, d2);
if(retval > 0) {
System.out.println("d1 is greater than d2");
} else if(retval < 0) {
System.out.println("d1 is less than d2");
} else {
System.out.println("d1 is equal to d2");
}
输出:
d1 is greater than d2
为什么?如何得到正确答案?
这是正确答案:2.4066419461049408E7
(到目前为止)大于 500.0
注意第一个数字末尾的E7
:这意味着x10^7
(指数)。
您因此将 24066419.461049408
与 500.0
进行比较。