I need the perfect algorithm or C# function to calculate the difference (distance) between 2 decimal numbers.
For example the difference between:
100>
This is how i do it in enterprise projects:
namespace Extensions
{
public class Functions
{
public static T Difference(object x1, object x2) where T : IConvertible
{
decimal d1 = decimal.Parse(x1.ToString());
decimal d2 = decimal.Parse(x2.ToString());
return (T)Convert.ChangeType(Math.Abs(d1-d2), typeof(T));
}
}
}
and testing:
namespace MixedTests
{
[TestClass]
public class ExtensionsTests
{
[TestMethod]
public void Difference_int_Test()
{
int res2 = Functions.Difference(5, 7);
int res3 = Functions.Difference(-3, 0);
int res6 = Functions.Difference(-3, -9);
int res8 = Functions.Difference(3, -5);
Assert.AreEqual(19, res2 + res3 + res6 + res8);
}
[TestMethod]
public void Difference_float_Test()
{
float res2_1 = Functions.Difference(5.1, 7.2);
float res3_1 = Functions.Difference(-3.1, 0);
double res5_9 = Functions.Difference(-3.1, -9);
decimal res8_3 = Functions.Difference(3.1, -5.2);
Assert.AreEqual((float)2.1, res2_1);
Assert.AreEqual((float)3.1, res3_1);
Assert.AreEqual(5.9, res5_9);
Assert.AreEqual((decimal)8.3, res8_3);
}
}
}