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
热门推荐
10 同事换单位简短祝福语
11 谢谢朋友的祝福语简短
12 立秋节气简短祝福语
13 新房祝福语简短10字
14 长辈新年祝福语大全简短
15 新年给大家简短祝福语
16 生日祝福语简短独特姑姑
17 芭比的婚礼祝福语简短
18 邀请新人祝福语简短英文