Search code examples
phpbehat

Dealing with percentages in PHP


I have a percentage logic problem and I don't really understand how to deal with it. Let me share with you:

I received a task where at some point I need to check if some pension income (its value I supposed) is less than 70% of the current income. And if so to set a flag. All good. The problem is that I received an example how the test should look like:

Given my income on my pension is <percentage>% of my current
Then the status should be <status>

Example:
|percentage| status |
|100       | true   |
|71        | true   |
|68        | false  |
|20        | false  |

I've created a function that finds this percentage because other way don't know how to get it, only in test is given dynamically values:

public function findPensionIncomePercentageFromTheCurrent()
{
    $pensionIncome = $this->getPensionIncome();

    $totalCurrentIncome = $this->getTotalCurrentIncome();

    if ($pensionIncome !== 0 && $totalCurrentIncome !== 0) {
        $percentage =  (int) round(($pensionIncome/$totalCurrentIncome) * 100, 0);

        return $percentage;
    }

    return false;
}

Ok, so this is the percentage. I also created another function that calculates 70% from the current income. And in the end, I tried to compare the percentage from the above function with the value from the 70% of pension income. But I realized that will work only if I multiply again the percentage with the current income like:

$currentIncome = $this->getTotalCurrentIncome();

$70percentageOfCurrentIncome = 70% * $currentIncome;

$result = $this->findPensionIncomePercentageFromTheCurrent() * $currentIncome;

if ($result < $70percentageOfCurrentIncome)
    $this->setTrue(true);
else {
    $this->setFalse(false);

Do you think it's okay how I did it? I am asking because I find a little weird to find the percentage by doing a/b * 100 and then that percentage to multiply again with b in order to get the result. I think something I'm not doing well.

Any suggestions?


Solution

  • To be truely technical a percentage is a number which corresponds to a proportion.

    So if you have an income of 1000 and a pension of 300 then the percentage of pension to income is 0.3 in numerical terms and not 30%.

    Here's what you should be doing:

    public function findPensionIncomePercentageFromTheCurrent()
    {
        $pensionIncome = $this->getPensionIncome();
    
        $totalCurrentIncome = $this->getTotalCurrentIncome();
    
        if ($pensionIncome !== 0 && $totalCurrentIncome !== 0) {
            $percentage =  ($pensionIncome/$totalCurrentIncome);
    
            return $percentage;
        }
    
        return false;
    }
    

    Then it would be a matter of:

     $currentIncome = $this->getTotalCurrentIncome();
    
     //0.7 is the real percentage value, 
     //70% is just something people use because we like whole numbers more
     $threshold = 0.7 * $currentIncome; 
    
     $result = $this->findPensionIncomePercentageFromTheCurrent() * $currentIncome;
    
     if ($result < $threshold)
       $this->setTrue(true);
     else {
       $this->setFalse(false);
    

    Now the caveat is if you need to show the percentage to people you need to do something like:

    echo round($percentage*100). "%"; //Would print something like 70%