Search code examples
c#algorithmmathnumbers

Difference between 2 numbers


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?


Solution

  • You can do it like this

    public decimal FindDifference(decimal nr1, decimal nr2)
    {
      return Math.Abs(nr1 - nr2);
    }