This sample shows two ways compare Java Double object with another Double object or two dobule primitive values using methods of java.lang.Double class
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 |
package com.javabycode.common; public class JavaDoubleCompare { public static void main(String[] args) { /* * It returns 0 if both the values are equal, returns value less than 0 if dNumber1 is less * than dNumber2, and returns value greater than 0 if dNumber1 is greater than dNumber2. */ double dNumber1 = 888.65; double dNumber2 = 888.64; int c1 = Double.compare(dNumber1, dNumber2); if (c1 > 0) { System.out.println("First number is greater"); } else if (c1 < 0) { System.out.println("Second number is greater"); } else { System.out.println("Both are equal"); } System.out.println("Value c1 = " + c1); /* * It returns 0 if both the values are equal, returns value less than 0 if dNumberObj1 is less * than dNumberObj2, and returns value greater than 0 if dNumberObj1 is greater than dNumberObj2. */ Double dNumberObj1 = new Double("888.64"); Double dNumberObj2 = new Double("888.65"); int c2 = dNumberObj1.compareTo(dNumberObj2); if (c2 > 0) { System.out.println("First number is greater"); } else if (c2 < 0) { System.out.println("Second number is greater"); } else { System.out.println("Both are equal"); } System.out.println("Value c2 = " + c2); } } |
Output
First number is greater
Value c1 = 1
Second number is greater
Value c2 = -1