Always round integer down and up

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Always round integer down and up

Post by batfastad »

Hi everyone
I wonder if you can help me with this, it's been frying my brain for a couple of hours :cry:

I have the ID of an item in a content management system, but I want to be checking that the author names the images properly according to the ID of the item, and puts them in the correct folder.

Folders are all named:
/500_599/
/600_699/
/700_799/
...
/1000_1099/
/1100_1199/
etc
So the images for post ID=1085, goes into the ../1000_1099/ folder
Normally I would use floor() and ceil() for this but they only operate on floating point rounding up/down to the whole number. It would be cool if there could be a precision parameter for floor/ceil like round() to give it a negative precision so they can operate on numbers like these.

Is there a way I can take the ID and round it down to get the lower limit, then round it up to get the upper limit of the folder name?

There's got to be a maths way of doing this, but I'm having real trouble here. Something like divide by 100 floor/ceil, then multiply by 100, subtracting 1 from ceil. But I just can't figure it out

Any ideas?

Thanks, B
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Always round integer down and up

Post by marcth »

Maybe divide the number by 100, apply a floor/ceil on it, then multiply the result by 100?

Code: Select all

<?php
$number = 569;
$folderNameA = (floor($number/100)*100) . '_' . (ceil($number/100)*100-1);
 
$number = 1111;
$folderNameB = (floor($number/100)*100) . '_' . (ceil($number/100)*100-1);
 
print $folderNameA . "<br />" . $folderNameB; // 500_599 1100_1199
?>
Last edited by marcth on Wed Sep 10, 2008 9:58 am, edited 1 time in total.
User avatar
Dravos
Forum Newbie
Posts: 15
Joined: Wed Sep 10, 2008 9:27 am
Location: London

Re: Always round integer down and up

Post by Dravos »

What you described seems to sound ok?

Code: Select all

$id = 541;
$lower=100*(floor($id/100));
$upper=100*(ceil($id/100))-1;
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Re: Always round integer down and up

Post by batfastad »

Dravos wrote:What you described seems to sound ok?

Code: Select all

$id = 541;
$lower=100*(floor($id/100));
$upper=100*(ceil($id/100))-1;
Yeah I'm almost there on that...
Apart from if you do ID=1100 or any other number that equals "lower"
User avatar
Dravos
Forum Newbie
Posts: 15
Joined: Wed Sep 10, 2008 9:27 am
Location: London

Re: Always round integer down and up

Post by Dravos »

Aye, you have to test for a remainder on $id/100 and if there is one do that, if there isn't do things slightly different.

Just trying to get this 2nd bit working :/
User avatar
Dravos
Forum Newbie
Posts: 15
Joined: Wed Sep 10, 2008 9:27 am
Location: London

Re: Always round integer down and up

Post by Dravos »

Here's a quick test file I wrote, edit the $id value to test.

Code: Select all

<?php
 
$id = 500;
 
if ($id % 100 != 0)
{
    $lower=100*(floor($id/100));
    $upper=100*(ceil($id/100))-1;
}
else
{
    $lower=$id;
    $upper=$id+99;
}
 
?>
 
Folder: <?=$lower?>_<?=$upper?>
User avatar
batfastad
Forum Contributor
Posts: 433
Joined: Tue Mar 30, 2004 4:24 am
Location: London, UK

Re: Always round integer down and up

Post by batfastad »

Yep that seems to work! :lol:
Thanks for your help :D

On a side note, I'm staggered that the round() function doesn't have that ability built in, to force the direction of rounding. Ah well, hopefully someone else will find this useful.

Thanks so much, Ben
Post Reply