Skip to main content

Posts

Showing posts with the label calculate percentages

How to calculate a percentage in php

In this tutorial we'll cover how to calculate a percentage using php and display the result with some decimal points. In the example below, we have two numbers. One is our base upon which our percentage will be calculated and the other is the value to determine the percentage. We will be using the number_format() function to determine how many decimal points our result will have, as well as to set the separator. $possible_mark = 100; $my_mark = 78; $percentage_achieved = ($my_mark / $possible_mark) * (100); $percentage_achieved = number_format((float)$percentage_achieved, 2, '.', ''); echo $percentage_achieved . "%"; The output from the above code will will be: 78.00%