Page 1 of 1

Always round integer down and up

Posted: Wed Sep 10, 2008 9:49 am
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

Re: Always round integer down and up

Posted: Wed Sep 10, 2008 9:54 am
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
?>

Re: Always round integer down and up

Posted: Wed Sep 10, 2008 9:56 am
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;

Re: Always round integer down and up

Posted: Wed Sep 10, 2008 9:59 am
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"

Re: Always round integer down and up

Posted: Wed Sep 10, 2008 10:05 am
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 :/

Re: Always round integer down and up

Posted: Wed Sep 10, 2008 10:08 am
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?>

Re: Always round integer down and up

Posted: Wed Sep 10, 2008 10:48 am
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