# calculate a percentage (dec) difference based on two numbers

this function will show you the percentage difference between two numbers

## PHP

``````// calc_percentage_dec.php

\$template_result = "";

// function - calc_percentage_dec(\$total, \$diff)
function calc_percentage_dec(\$total, \$diff) {
\$dec = \$total - \$diff;
return \$dec_percent = ( \$dec / \$total ) * 100;
}

// Usage examples, as shown in demo.:

// total
\$total = 2406;
\$diff = 356;

// % difference
\$perc_diff = calc_percentage_dec(\$total, \$diff);
\$perc_diff_rounded = number_format(\$perc_diff, 2);

\$template_result .= "<p>The percentage difference between <b>\$total</b> and <b>\$diff</b> is <b>\$perc_diff%</b></p>";
\$template_result .= "<p>You can round this number down using number_format(\$perc_diff, 2) which should give you <b>\$perc_diff_rounded</b></p>";

// and another more simple one to test the numbers are calculating correctly

// total
\$total = 100;
\$diff = 50;
\$perc_diff = calc_percentage_dec(\$total, \$diff);

\$template_result .= "<p>The percentage difference between \$total and \$diff is: \$perc_diff%</p>";``````

## Demo

The percentage difference between 2406 and 356 is 85.20365752286%

You can round this number down using number_format(85.20365752286, 2) which should give you 85.20

The percentage difference between 100 and 50 is: 50%

##### Page Views

No Items Found.

functions ##### calculate a percentage (dec) difference based on two numbers

This function takes two numbers and returns the difference in percentage.

1:26 am, January 20, 2020
##### Search Code
Search Code by entering your search text above.
##### Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹