How to calculate a percentage
Short answer
A percentage of a number — multiply and divide by a hundred:
<?php
$price = 1200;
echo $price * 15 / 100;Output:
180What share a number makes up
Divide the part by the whole:
<?php
echo round(45 / 180 * 100, 1), '%';Output:
25%A percentage change
The difference is divided by the old value:
<?php
$old = 200;
$new = 250;
echo round(($new - $old) / $old * 100), '%';Output:
25%Functions used in this recipe
- round — Rounds a number to a given number of digits.
- number_format — Formats a number for display: decimals and a thousands separator.