I have these two numbers original one is 1000
the new one is 440
I need to calculate the percentage between them as follow:
1000
440
The percentage that getting is 44% and left is 56%
my helper function:
function calculatePercentage($original, $given)
{
$percentChange = (($original - $given) / $original) * 100;
return round(abs($percentChange)) . '%';
}
This gives me the reset percentage!
CodePudding user response:
use this
function calculatePercentage($original, $given)
{
$percentageOfGiven = round(abs( ($given * 100) / $original ));
return [
'getting' => $percentageOfGiven . '%',
'left' => (100 - $percentageOfGiven) . '%'
];
}
The output will be like [ 'getting' => "44%", 'left' => "56%"]
.
The principle used is that if the original is 100% then what would be the given value.
CodePudding user response:
Wouldn't it be like
function calculatePercentage($original, $given)
{
$percentChange = ($given / $original) * 100;
return round(abs($percentChange)) . '%';
}
If I am wrong, I probably didn't understand ques enough