-3

I have two double values, and I want to find which double is bigger as either one could be bigger number.

For example

Double a = Value1;
Double b = Value2;

Either could be bigger. I need to find out which one is bigger to find the difference between them.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
Grbe1l
  • 489
  • 4
  • 16

3 Answers3

3

You can compare doubles with the < or > operator.

But you want to get the difference. That can be done simply by taking the absolute value of the subtraction of either one from the other.

Double difference = Math.abs( val1 - val2 );
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
0

You can use compareTo, Double is implementing Comparable interface.

Fernando Garcia
  • 1,946
  • 2
  • 12
  • 14
0

There are many ways to do this, but I would recommend using Double.compare(val1,val2);, as this is the express purpose for which this method was designed.

This method returns an integer. If the integer is 0 then the two doubles are equal. If the integer is less than 0, then val1 is less than val2, and if if the integer is greater than 0, val1 is greater than val2.

stubsthewizard
  • 352
  • 2
  • 12