Difference between 2 numbers


Question

I need the perfect algorithm or C# function to calculate the difference (distance) between 2 decimal numbers.

For example the difference between:
100 and 25 is 75
100 and -25 is 125
-100 and -115 is 15
-500 and 100 is 600

Is there a C# function or a very elegant algorithm to calculate this or I have to go and handle every case separately with ifs.

If there is such a function or algorithm, which one is it?

1
61
7/21/2012 7:58:04 AM

Accepted Answer

You can do it like this

public decimal FindDifference(decimal nr1, decimal nr2)
{
  return Math.Abs(nr1 - nr2);
}
108
12/5/2008 8:34:29 AM

result = Math.Abs(value1 - value2);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon