PHP: Percentage of a number

74

I could never think it would be even challenging but is there any function to calculate a percentage of a number or do I have to write a custom function?

Example:
$result=percent(25,100); //doing the 100%25 operation
$result = 4

305

Answer

Solution:

function percentage($part, $whole = 100) {
     settype($part, "float");
     settype($whole, "float");
     $formule = ($part / $whole) * 100;
     return $formule . " %";
}

echo percentage(25); // returns 25 %
echo percentage(91.8, 176); // returns 52,15909090909091 %

Voila ;-) Could be improved more, but i saw that you figured it out for yourself, so not needed anymore..

People are also looking for solutions to the problem: php - insert foreign key value from parent table into child table

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.