PHP calculate percentages
Asked Answered
V

3

7

I need some help. It's a simple code, but I don't have idea how to write in down. I have the numbers:

$NumberOne = 500;
$NumberTwo = 430;
$NumberThree = 150;
$NumberFour = 30; 

At all this is:

$Everything = 1110; // all added

Now I want to show what percentage is for example $NumberFour of everything or what percentage is $NumberTwo of $Everything. So the "market share".

Vasili answered 21/3, 2015 at 10:36 Comment(4)
Have you tried anything?Backstay
If you find that you're numbering variables, then you should probably be using an array insteadDigitalin
Hint: Percentage is (part/total) * 100.Sirdar
demoDigitalin
B
33

Use some simple maths: divide the number you wish to find the percentage for by the total and multiply by 100.

Example:

$total = 250;
$portion = 50;
$percentage = ($portion / $total) * 100; // 20

Solution to original example

To get $NumberFour as a percentage of the total amount you'd use:

$percentage = ($NumberFour / $Everything) * 100;

Rounding

Depending on the numbers you're working with, you may want to round the resulting percentage. In my initial example, we get 20% which is a nice round number. The original question however uses 1110 as the total and 30 as the number to calculate the percentage for (2.70270...).

PHP's built in round() function could be useful when working with percentages for display: https://www.php.net/manual/en/function.round.php

echo round($percentage, 2) . '%'; // 2.7% -- (30 / 1110) * 100 rounded to 2dp

Helper Functions

I would only contemplate creating helper functions when their use justifies it (if calculating and displaying a percentage isn't a one-off). I've attached an example below tying together everything from above.

function format_percentage($percentage, $precision = 2) {
    return round($percentage, $precision) . '%';
}

function calculate_percentage($number, $total) {

    // Can't divide by zero so let's catch that early.
    if ($total == 0) {
        return 0;
    }

    return ($number / $total) * 100;
}

function calculate_percentage_for_display($number, $total) {
    return format_percentage(calculate_percentage($number, $total));
}

echo calculate_percentage_for_display(50, 250); // 20%
echo calculate_percentage_for_display(30, 1110); // 2.7%
echo calculate_percentage_for_display(75, 190); // 39.47%
Blindworm answered 21/3, 2015 at 10:40 Comment(0)
V
7

Create function to calculate percentage between two numbers.

<?php

/**
 * Calculate percetage between the numbers
 */

function percentageOf( $number, $everything, $decimals = 2 ){
    return round( $number / $everything * 100, $decimals );
}

$numbers = array( 500, 430, 150, 30 );
$everything = array_sum( $numbers );

echo 'First of everything: '.percentageOf( $numbers[0], $everything )."%\n";
echo 'Second of everything: '.percentageOf( $numbers[1], $everything )."%\n";
echo 'Third of everything: '.percentageOf( $numbers[2], $everything )."%\n";
echo 'Fourth of everything: '.percentageOf( $numbers[3], $everything )."%\n";

?>

This outputs

First of everything: 45.05%
Second of everything: 38.74%
Third of everything: 13.51%
Fourth of everything: 2.7%
Vacate answered 21/3, 2015 at 10:50 Comment(3)
$numbers = array( 500, 200, 500, 200,100 ); for this array, the total percentage sum is not equal 100, but 99.99 .its always .01 shortageAirmail
@RockersTechnology It depends. It could be, yes, because it's rounding to two decimals. You can always show more decimalsVacate
@RockersTechnology yeah, you are basically chopping parts from each calculation using rounding. So, getting 0.01 short is not a problem.Thickwitted
F
-1

You can also come up with the percentage with the following formula. For the percentage, add a 0. in front of it. So 5% would be 0.05. Total X 0.05 is the amount.

Fitful answered 6/9, 2022 at 11:53 Comment(1)
It's same to the answers above. If I want to get 10% of x, I'll multiply it with 0.1.Thickwitted

© 2022 - 2025 — McMap. All rights reserved.