Compare two double values

ReturnMethodSummary
static intcompare(double d1, double d2)Compares the two specified double values.
intcompareTo(Double anotherDouble)Compares two Double objects numerically.
booleanequals(Object obj)Compares this object against the specified object.

static int compare(double d1, double d2) and int compareTo(Double anotherDouble) have the same returns:

ValueMeaning
0if anotherDouble is numerically equal to this Double
less than 0if this Double is numerically less than anotherDouble;
value greater than 0if this Double is numerically greater than anotherDouble.

public class Main {
  public static void main(String[] args) {
    Double double1 = new Double(1.1);
    Double double2 = new Double("1.2");

    System.out.println(double1.compareTo(double2));

  }
}

The output:


-1

public class Main {
  public static void main(String[] args) {
    Double double1 = new Double(1.1);
    Double double2 = new Double("1.2");

    System.out.println(Double.compare(double1,double2));

  }
}

The output:


-1
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.