|
|
| Craig Keightley 2004-09-24, 4:00 pm |
| Is there a way to "round" (bad use of the word) a number to the nearest 5
eg
4 => 5
7 => 10
1 => 5
12 => 15
Many Thanks for any help provide
Craig
| |
| J.O. Aho 2004-09-24, 4:00 pm |
| Craig Keightley wrote:
> Is there a way to "round" (bad use of the word) a number to the nearest 5
> eg
> 4 => 5
> 7 => 10
> 1 => 5
> 12 => 15
function round5($val) {
$div=floor((int)$val/(int)5);
if($val%5) {
$div++;
}
return $div * 5;
}
| |
|
| Try this sample:
for($x=0;$x<30;$x++)
{
if( !($x % 5) ) // if mod is zero, we're already there
{
$nearest = $x;
}
else
{
$nearest = (intval($x/5)+1)*5;
}
echo "\n $x rounds to $nearest";
}
"Craig Keightley" <dont@spam.me> wrote in
news:41542f09$0$20250$cc9e4d1f@news-text.dial.pipex.com:
> Is there a way to "round" (bad use of the word) a number to the nearest 5
> eg
> 4 => 5
> 7 => 10
> 1 => 5
> 12 => 15
>
> Many Thanks for any help provide
> Craig
>
>
>
| |
|
| Or as a function:
function another_round5($x)
{
if( !($x % 5) )
return($x);
else
return((intval($x/5)+1)*5);
}
Ehtor <nomail@nomail.com> wrote in
news:Xns956E750BD82C5nomailnomailcom@140
.99.99.130:
> Try this sample:
>
> for($x=0;$x<30;$x++)
> {
> if( !($x % 5) ) // if mod is zero, we're already there
> {
> $nearest = $x;
> }
> else
> {
> $nearest = (intval($x/5)+1)*5;
> }
>
> echo "\n $x rounds to $nearest";
> }
>
>
>
> "Craig Keightley" <dont@spam.me> wrote in
> news:41542f09$0$20250$cc9e4d1f@news-text.dial.pipex.com:
>
>
>
| |
| Craig Keightley 2004-09-24, 4:00 pm |
| Thanks all but i went with this:
$round_x = 5 * ceil($x/5);
"Ehtor" <nomail@nomail.com> wrote in message
news:Xns956E75DFD247Fnomailnomailcom@140
.99.99.130...
> Or as a function:
>
> function another_round5($x)
> {
> if( !($x % 5) )
> return($x);
> else
> return((intval($x/5)+1)*5);
> }
>
>
>
> Ehtor <nomail@nomail.com> wrote in
> news:Xns956E750BD82C5nomailnomailcom@140
.99.99.130:
>
>
| |
| Hilarion 2004-09-24, 4:00 pm |
| > $round_x = 5 * ceil($x/5);
Your code gives 10 as output for 6 input, where nearest 5
should be 6.
Try this:
$round_x = 5 * round( $x / 5 );
Hilarion
| |
| Nikolai Chuvakhin 2004-09-24, 8:56 pm |
| "Craig Keightley" <dont@spam.me> wrote in message
news:<41542f09$0$20250$cc9e4d1f@news-text.dial.pipex.com>...
>
> Is there a way to "round" (bad use of the word) a number
> to the nearest 5 eg
> 4 => 5
> 7 => 10
> 1 => 5
> 12 => 15
Sure:
$oldnumber = 12;
$newnumber = ceil($oldnumber / 5) * 5; // $newnumber = 15...
Cheers,
NC
| |
| Craig Keightley 2004-09-27, 8:55 am |
| Thats what i want it to do:
Round Up
e.g 6 <= 10
therefore 6 == 10
"Hilarion" <hilarion@SPAM.op.SMIECI.pl> wrote in message
news:cj1h56$c9u$1@news.onet.pl...
>
> Your code gives 10 as output for 6 input, where nearest 5
> should be 6.
>
> Try this:
>
> $round_x = 5 * round( $x / 5 );
>
> Hilarion
>
|
|
|
|