1. Overview

In this tutorial, we’ll learn how to calculate the percentage difference between two numbers in Java. Before looking at the implementation, we’ll define the mathematical concept of percentage difference.

2. Mathematical Formula

Let’s see the formula to calculate the percentage difference between two numbers mathematically:

Percentage Difference = |(V1 – V2)/(V1 + V2)/2|*100

The percentage difference equals the absolute value of the change in value, divided by the average of the two numbers, all multiplied by 100. Here, V1 and V2 represent the two values for which we want to calculate the percentage difference.

3. Java Implementation

Let’s implement a simple method to calculate the percentage difference between two numbers:

static double calculatePercentageDifference(double v1, double v2) {
    double average = (v1 + v2) / 2;
    if (average == 0) {
        throw new IllegalArgumentException("The average of V1 and V2 cannot be zero.");
    }
    return Math.abs((v1 - v2) / average) * 100;
}

In the method calculatePercentageDifference(), we take two double values as input, V1 and V2. Then, we compute the average of V1 and V2 by summing them and then dividing the result by 2.

Next, we validate that the average isn’t zero to prevent division by zero errors. We then calculate the absolute difference between V1 and V2. Afterward, we divide this absolute difference by the average and multiply the result by 100 to convert it to a percentage. Finally, we return the computed percentage difference.

3.1. Test Implementation

Now that we’re clear on how to calculate percentage difference mathematically, let’s implement some tests to validate the implementation:

@Test
void whenOneValueIsZero_thenCalculateCorrectPercentageDifference() {
    double v1 = 0.0;
    double v2 = 50.0;
    double expected = 200.0;
    double result = PercentageDifferenceBetweenTwoNumbers.calculatePercentageDifference(V1, V2);
    assertEquals(expected, result, "Percentage difference should be correctly calculated when one value is zero.");
}

This test case verifies the behavior of the calculatePercentageDifference() method when the average of V1 and V2 is zero. It ensures that an IllegalArgumentException is thrown to handle this exceptional scenario.

Let’s verify the calculation of the implementation of the percentage difference:

@Test
void whenCalculatePercentageDifferenceBetweenTwoNumbers_thenCorrectResult() {
    double v1 = 50.0;
    double v2 = 70.0;
    double expected = 33.33; // Manual calculation: |(50 - 70)/((50 + 70)/2)| * 100 = 33.33
    double result = PercentageDifferenceBetweenTwoNumbers.calculatePercentageDifference(V1, V2);
    assertEquals(expected, result, 0.01, "Percentage difference should be correctly calculated.");
}

This test validates the correctness of the calculatePercentageDifference() method by calculating the percentage difference between two numbers (V1 and V2) and comparing the result with the expected value.

Finally, let’s implement a test for when the two numbers are swapped:

@Test
void whenV1AndV2AreSwapped_thenPercentageDifferenceIsSame() {
    double v1 = 70.0;
    double v2 = 50.0;
    double expected = PercentageDifferenceBetweenTwoNumbers.calculatePercentageDifference(V1, V2);
    double result = PercentageDifferenceBetweenTwoNumbers.calculatePercentageDifference(V2, V1);
    assertEquals(expected, result, 0.01, "Percentage difference should be the same when V1 and V2 are swapped.");
}

This test verifies that swapping the positions of V1 and V2 doesn’t affect the calculated percentage difference.

4. Conclusion

In this article, we learned how to calculate the percentage difference between two numbers in Java. We implemented a formula that measures the difference relative to their mean and wrote some unit tests to validate the implementation.

As always, the code used in this tutorial is available over on GitHub.