Compare Double Value - Java Language Basics

Java examples for Language Basics:double

Description

Compare Double Value

Demo Code

 
public class Main {
 
  public static void main(String[] args) {
    double d1 = 5.35;
    double d2 = 5.34;
    int i1 = Double.compare(d1,d2);
   /*from  ww w . j a va  2 s.c  om*/
    if(i1 > 0){
      System.out.println("First is greater");
    }else if(i1 < 0){
      System.out.println("Second is greater");
    }else{
      System.out.println("Both are equal");
    }
   
    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 greater");
    }else if(i2 < 0){
      System.out.println("Second is greater");
    }else{
      System.out.println("Both are equal");
    }    
  }
}

Result


Related Tutorials