views:

85

answers:

2

Lets say I divide 14 / 15 times it by 100 to get the percentage which is 93.33333333333333 how can I display it as 93.3% using php?

Here is the code.

$percent = ($avg / 15) * 100;
+8  A: 

The sprintf function is made for this (see also the manual):

echo sprintf('%.1f%%', $percent);
Wim
Depending on what you're looking for, round() ( http://php.net/manual/en/function.round.php ) is also a good option.
Robert Elwell
+8  A: 

PHP has a number_format function which lets you specify the number of decimals, what to use for the decimal separator, and what to use for the thousands separator:

$percent = ($avg / 15) * 100;
echo number_format($percent, 1) . '%'; // => 93.3%
Daniel Vandersluis