Java - Double compare() method



Description

The Java Double compare() method compares the two specified double values. The sign of the integer value returned is the same as that of the integer that would be returned by the call −

new Double(d1).compareTo(new Double(d2))

Declaration

Following is the declaration for java.lang.Double.compare() method

public static int compare(double d1, double d2)

Parameters

  • d1 − This is the first double to compare

  • d2 − This is the second double to compare.

Return Value

This method returns the value 0 if d1 is numerically equal to d2; a value less than 0 if d1 is numerically less than d2; and a value greater than 0 if d1 is numerically greater than d2.

Exception

NA

Example 1

The following example shows the usage of Double compare() method to check a value is greater than another value or not. We're having two double values and using compare() method, we're comparing these double values and then result is compared to 0. If result is greater than 0 then first number is greater than second number. If result is less than 0 then first number is less than second number. Otherwise both values are same.

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {

      // compares the two specified double values
      double d1 = 15.45;
      double d2 = 11.50;
      int retval = Double.compare(d1, d2);
    
      if(retval > 0) {
         System.out.println("d1 is greater than d2");
      } else if(retval < 0) {
        System.out.println("d1 is less than d2");
      } else {
         System.out.println("d1 is equal to d2");
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

d1 is greater than d2

Example 2

The following is another example to show the usage of Double compare() method to check a value is less than another value or not. We're having two double values and using compare() method, we're comparing these double values and then result is compared to 0. If result is greater than 0 then first number is greater than second number. If result is less than 0 then first number is less than second number. Otherwise both values are same.

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {

      // compares the two specified double values
      double d1 = 11.50;
      double d2 = 15.45;
      int retval = Double.compare(d1, d2);
    
      if(retval > 0) {
         System.out.println("d1 is greater than d2");
      } else if(retval < 0) {
        System.out.println("d1 is less than d2");
      } else {
         System.out.println("d1 is equal to d2");
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

d1 is less than d2

Example 3

The following example shows the usage of Double compare() method to check a value is same as another value or not. We're having two double values and using compare() method, we're comparing these double values and then result is compared to 0. If result is greater than 0 then first number is greater than second number. If result is less than 0 then first number is less than second number. Otherwise both values are same.

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {

      // compares the two specified double values
      double d1 = 15.45;
      double d2 = 15.45;
      int retval = Double.compare(d1, d2);
    
      if(retval > 0) {
         System.out.println("d1 is greater than d2");
      } else if(retval < 0) {
        System.out.println("d1 is less than d2");
      } else {
         System.out.println("d1 is equal to d2");
      }
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

d1 is equal to d2
java_lang_double.htm
Advertisements