1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
/* Java Double compare example This example shows how to compare a Double object with other Double object, Double with an Object, or two double primitive values using methods provided by java.lang.Double class. */ public class JavaDoubleCompareExample { public static void main(String[] args) { /* To compare two double primitive values use compare(double d1, double d2) method of Double class. This is a static method. It returns 0 if both the values are equal, returns value less than 0 if d1 is less than d2, and returns value grater than 0 if d1 is grater than d2. */ double d1 = 5.35; double d2 = 5.34; int i1 = Double.compare(d1,d2); if(i1 > 0){ System.out.println("First is grater"); }else if(i1 < 0){ System.out.println("Second is grater"); }else{ System.out.println("Both are equal"); } /* To compare a Double object with another Double object use int compareTo(Double d) method. It returns 0 if both the values are equal, returns value less than 0 if this Double object is less than the argument, and returns value grater than 0 if this Double object is grater than the argument. */ Double dObj1 = new Double("5.35"); Double dObj2 = new Double("5.34"); int i2 = dObj1.compareTo(dObj2); if(i2 > 0){ System.out.println("First is grater"); }else if(i2 < 0){ System.out.println("Second is grater"); }else{ System.out.println("Both are equal"); } } } /* Output would be First is grater First is grater */ |
This has been very helpful to me. Greetings from Lima – Peru.
good one!! It helped!