Skip to content

Instantly share code, notes, and snippets.

@brunogaspar brunogaspar/difference.md
Last active Jan 12, 2018

Embed
What would you like to do?
[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
You can’t perform that action at this time.