getButterfly Logo getButterfly code wrangling since 2005

Sometimes called the rule of three, this is how you calculate the percentage of a number from another number.

For example, you have 2 numbers, 34 and 116. Let’s say 116 is the total number (100%), and we want to find out what percentage is 34 out of 100%.

Here’s how:

function percentage($amount, $total, $decimal = 2) {
	if(0 === (int)$total) {
		return $total;
	return number_format((((int)$amount / (int)$total) * 100), $decimal);

echo percentage(34, 116) . '%';

It’s simple math.

Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.

If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

Leave a reply