mysql - Error in this php code calculation not sure it does not produce the expected resultsGet the solution ↓↓↓
I am trying to run this code to give me the percentage of two whole numbers.
The code gives me the correct percentage, but does not round the result to a whole number.
For example: 84.33333 to 85.
I am new to PHP and have been unable to create the right code to round up or down for that matter, I have tried floor and ceil.
($result['sales']) / ($result['appointments']) *100, round(0.05)
Any help to point me in the right direction would be appreciated.
If you want to round your fractions up then use
<?php echo ceil(4.3); // 5 echo ceil(9.999); // 10 echo ceil(-3.14); // -3 ?>
If you want to round your number to some pre-defined fractions then use
round function have a second parameter called
precision which can be used for this purpose.
<?php var_dump(round(3.4)); // float(3) var_dump(round(3.5)); // float(4) var_dump(round(3.6)); // float(4) var_dump(round(3.6, 0)); // float(4) var_dump(round(1.95583, 2)); // float(1.96) var_dump(round(1241757, -3)); // float(1242000) var_dump(round(5.045, 2)); // float(5.05) var_dump(round(5.055, 2)); // float(5.06) ?>
You will get more information from the PHP Docs. Links below.
For your specific case it will be like below:
$percentage = ( $result['sales'] * 100 ) / $result['appointments']; $percentage_rounded = ceil($percentage); var_dump($percentage_rounded); exit;
the function you're looking for is
People are also looking for solutions of the problem: undefined array key
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.
Find the answer in similar questions on our website.
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.