How to Compare Size of Two Double Numbers in Java


This tutorial will teach how to compare a Double object with other Double object, Double with an Object, or two double primitive values with methods provided by java.lang.Double class.

Source Code

public class CompareDoubleNumber {

	public static void main(String[] args) {

		double n1 = 3.14;
		double n2 = 3.02;

		if (n1 > n2) {

			System.out.println("n1>n2");

		} else if (n1 < n2) {
			
			System.out.println("n1n4");
			
			break;
		case 0:System.out.println("n3==n4");
		
			break;
		case -1:System.out.println("n3<n4");
		
			break;
		}

	}

}

Output:

n1>n2
n3>n4
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments