Java Double比较示例
要在Java中比较Double,请使用以下方法-
方法1-compareTo()Java中的方法
java.lang.Double.compareTo()方法比较两个Double对象。
示例
import java.lang.*;
public class Demo {
public static void main(String args[]) {
Double d1 = new Double("395.24");
Double d2 = new Double("323.30");
int res = d1.compareTo(d2);
if(res > 0) {
System.out.println("d1 is greater than d2");
}
else if(res < 0) {
System.out.println("d1 is less than d2");
}
else {
System.out.println("d1 is equal to d2");
}
}
}输出结果
d1 is greater than d2
方法2-compare()Java中的方法
java.lang.Double.compare()方法以数字方式比较两个double值对象。
示例
import java.lang.*;
public class Demo {
public static void main(String args[]) {
Double d1 = new Double("195.24");
Double d2 = new Double("323.30");
int res = Double.compare(d1, d2);
if(res > 0) {
System.out.println("d1 is greater than d2");
}
else if(res < 0) {
System.out.println("d1 is less than d2");
}
else {
System.out.println("d1 is equal to d2");
}
}
}输出结果
d1 is less than d2
热门推荐
3 十月简短祝福语
10 送笔筒的祝福语简短
11 生女儿的祝福语简短
12 给婴儿祝福语简短的话
13 红颜生日祝福语简短独特
14 婆婆生日祝福语简短的
15 老公开业祝福语简短
16 新年祝福语短句简短搞笑
17 特别的端午祝福语简短
18 新年给妹妹祝福语简短