How to compare a Double object with other Double object in Java

2 Answers

0 votes
package javaapplication1;

public class JavaApplication1 {
 
    public static void main(String[] args) {
  
        Double d1 = new Double("3.14159265");
        Double d2 = new Double("3.14159266");
        
        int i = d1.compareTo(d2);

        if (i > 0) {
            System.out.println("d1 > d2");
        } else if (i < 0) {
            System.out.println("d2 > d1");
        } else {
            System.out.println("d1 = d2");
        }
    }
}
 
/*
    
run:
    
d2 > d1

*/

 



answered Nov 3, 2016 by avibootz
edited Nov 4, 2016 by avibootz
0 votes
package javaapplication1;
 
public class JavaApplication1 {
  
    public static void main(String[] args) {
   
        Double d1 = new Double("3.1415926");
        Double d2 = new Double("3.1415925");
 
        if (d1 > d2) {
            System.out.println("d1 > d2");
        } else if (d2 > d1) {
            System.out.println("d2 > d1");
        } else {
            System.out.println("d1 = d2");
        }
    }
}
  
/*
     
run:
     
d1 > d2
 
*/

 



answered Nov 4, 2016 by avibootz

Related questions

2 answers 173 views
1 answer 147 views
2 answers 189 views
1 answer 191 views
1 answer 163 views
...