1

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?

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
IleNea
  • 569
  • 4
  • 17
  • 4
    Possible duplicate of [How do I calculate the percentage of a number?](https://stackoverflow.com/questions/10201027/how-do-i-calculate-the-percentage-of-a-number) – Aleksa Arsić Nov 05 '17 at 08:50
  • Does this actually work? `$70percentageOfCurrentIncome = 70% * $currentIncome;` looks like it's going to throw all sorts of errros – apokryfos Nov 05 '17 at 09:10
  • @apokryfos anxiously it is: return (int) round(($percentage/100) * $salaryIncome, 0); where percentage is 70 – IleNea Nov 05 '17 at 09:11
  • @Mark Lenon, I ve seen that but still don't understand how to do in my case – IleNea Nov 05 '17 at 09:14
  • The problem is that 70% is not a number and not a percentage either (as far as PHP knows). It's something people write because it looks nicer to the eye. – apokryfos Nov 05 '17 at 09:20

1 Answers1

1

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%
apokryfos
  • 38,771
  • 9
  • 70
  • 114