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?
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.
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).
You can do it like this
public decimal FindDifference(decimal nr1, decimal nr2) { return Math.Abs(nr1 - nr2); }
result = Math.Abs(value1 - value2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With