Reputation: 26499
I want to round up any an integer to the next tens place number.
Some examples to illustrate my point:
-- I have the number 1, I would like this rounded up to 10
-- I have the number 35, I would like this rounded up to 40
-- I have the number 72, I would like this rounded up to 80
-- etc etc
// $category_count's value is 38
for($i = 1; $i <= $category_count; $i++)
{
if($i % 10 == 0)
{
echo "<a href=\"?page=$i\">$i;</a>";
}
}
The above code outputs 3 links, I need the fourth too.
Upvotes: 0
Views: 7931
Reputation: 149
round(($num/10))*10; // Make sure num is an integer or use (int) to convert string to integer.
This works for me :)
Upvotes: 1
Reputation: 39872
Edited my answer to point to this question instead. Basically same thing with many good answers.
How to round up a number to nearest 10?
Upvotes: 2
Reputation: 53526
Your for
is ineffective. If you need to modulus 10 your counter, use this code instead :
for($i = 1, $c = ceil($category_count/10); $i <= $c; $i++)
{
$j = $i * 10;
echo "<a href=\"?page=$j\">$j;</a>";
}
Upvotes: 3
Reputation: 7680
mrtsherman is almost right but OP's question needed it to round UP (1 => 10)
// $category_count's value is 38
$loop_limit = ceil($category_count/10);
for ($i = 1; $i <= $loop_limit; $i++) {
$page = $i * 10;
echo "<a href=\"?page={$page}\">{$page}</a>";
}
Upvotes: 2
Reputation: 881303
One way to do this is to add 9, then truncate it at the tens place (integer divide by ten, then multiply by ten).
Alternatively, you can add 5 then use the round
function with a negative precision:
echo round ($i + 5, -1);
Upvotes: 0