Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 921
Germstorm Avatar asked Dec 04 '08 09:12

Germstorm


People also ask

How do I find the percentage difference between 2 numbers?

Percentage Difference Formula The percentage difference between two values is calculated by dividing the absolute value of the difference between two numbers by the average of those two numbers. Multiplying the result by 100 will yield the solution in percent, rather than decimal form.

What does it mean by the difference between 2 numbers?

In math, the word difference is the result of subtracting one number from another. It refers to the difference in quantity between two numbers. In math, we get the difference between two numbers by subtracting the subtrahend (the number being subtracted) from the minuend (the number being subtracted from).


2 Answers

You can do it like this

public decimal FindDifference(decimal nr1, decimal nr2) {   return Math.Abs(nr1 - nr2); } 
like image 158
terjetyl Avatar answered Sep 28 '22 04:09

terjetyl


result = Math.Abs(value1 - value2); 
like image 26
Martin Avatar answered Sep 28 '22 03:09

Martin