Skip to content

Instantly share code, notes, and snippets.

@brunogaspar
Last active October 13, 2020 15:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunogaspar/7e990eaae6f130b845e4e81c09abc26e to your computer and use it in GitHub Desktop.
Save brunogaspar/7e990eaae6f130b845e4e81c09abc26e to your computer and use it in GitHub Desktop.
[PHP] Percentage Calculation Helpers

Calculate Difference

if (! function_exists('calculatePercentage')) {
    /**
     * Calculates the percentage between the given values.
     *
     * @param int|string $previous
     * @param int|string $current
     *
     * @return string
     */
    function calculatePercentage($previous, $current)
    {
        if (! isset($previous, $current) || $current === 0) {
            return null;
        }

        return $previous / $current;
    }
}

Unit Tests

public function test_the_calculate_percentage_helper()
{
    $values = [
        ['previous' => 0, 'current' => 0, 'expects' => null],
        ['previous' => 0, 'current' => 1, 'expects' => 0],
        ['previous' => 1, 'current' => 0, 'expects' => null],
        ['previous' => 1, 'current' => 1, 'expects' => 1],
        ['previous' => 0, 'current' => -1, 'expects' => 0],
        ['previous' => -1, 'current' => 0, 'expects' => null],
        ['previous' => -1, 'current' => -1, 'expects' => 1],
        ['previous' => 0.50, 'current' => 0.30, 'expects' => 1.6666666666666667],
        ['previous' => 0.50, 'current' => -0.30, 'expects' => -1.6666666666666667],
        ['previous' => 4466.43, 'current' => 4212.79, 'expects' => 1.0602071311411203],
        ['previous' => 1068.00, 'current' => 535.00, 'expects' => 1.9962616822429906],
        ['previous' => 76.39, 'current' => 76.51, 'expects' => 0.9984315775715592],
        ['previous' => 135, 'current' => 360, 'expects' => 0.375],
    ];

    foreach ($values as $value) {
        $percentage = calculatePercentage($value['previous'], $value['current']);

        $this->assertSame($value['expects'], $percentage);
    }
}

Calculate Increase / Decrease

if (! function_exists('calculatePercentageIncrease')) {
    /**
     * Calculates the percentage increase/decrease for the given values.
     *
     * @param int|string $previous
     * @param int|string $current
     *
     * @return string
     */
    function calculatePercentageIncrease($previous, $current)
    {
        if (! isset($previous, $current) || $previous === 0) {
            return null;
        }

        return ($current - $previous) / $previous;
    }
}

Unit Tests

public function test_the_calculate_percentage_increase_helper()
{
    $values = [
        ['previous' => 0, 'current' => 0, 'expects' => null],
        ['previous' => 0, 'current' => 1, 'expects' => null],
        ['previous' => 1, 'current' => 0, 'expects' => -1],
        ['previous' => 1, 'current' => 1, 'expects' => 0],
        ['previous' => 0, 'current' => -1, 'expects' => null],
        ['previous' => -1, 'current' => 0, 'expects' => -1],
        ['previous' => -1, 'current' => -1, 'expects' => 0],
        ['previous' => 0.50, 'current' => 0.30, 'expects' => -0.40],
        ['previous' => 0.50, 'current' => -0.30, 'expects' => -1.60],
        ['previous' => 4466.43, 'current' => 4212.79, 'expects' => -0.056788083547710436],
        ['previous' => 1068.00, 'current' => 535.00, 'expects' => -0.499063670411985],
        ['previous' => 76.39, 'current' => 76.51, 'expects' => 0.0015708862416547265],
        ['previous' => 135, 'current' => 360, 'expects' => 1.6666666666666667],
    ];

    foreach ($values as $value) {
        $percentage = calculatePercentageIncrease($value['previous'], $value['current']);

        $this->assertSame($value['expects'], $percentage);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment