Skip to content
Advertisement

How to show round figure number in php?

I have a variable called $totalResults which has dynamic values, depend on database entries. I need to show a limit like if $totalResults has 154 results then I want show that 154 out of 500. But I want it dynamically, like if the result cross 500, example the result value is 514, then it’ll show like 514 out of 1000.

I don’t want to use if, else conditions because the result will gradually increase, so i need to compare it dynamically, Like if result has 1500 entries, then I’ll add +500 into it, Then it’ll show Like 1500 out of 2000.

I am confused in this part, If the result is not round figure, how can I make it right?

My Logic:

$totalResult = 154;
$newResult = $totalResult + 500;

//Result = 654
But I want to show this 154 out of 500 or 154 out of 1000

Advertisement

Answer

You can use the ceil function

$totalResult = 154;
$coparisonQuotient = ceil($totalresults / 500);
$outOf = $comparisonQuotient * 500;

echo $totalResult . " out of " . $outOf;

Now, if the total results are 154, it would display 154 out of 500

If it is above 500, but less than 1000, let’s say 784. It will display 784 out of 1000

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement