# Number question

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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

## Re: Number question

Sure:

\$oldnumber = 12;
\$newnumber = ceil(\$oldnumber / 5) * 5; // \$newnumber = 15...

Cheers,
NC

## Re: Number question

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";
}

## Re: Number question

Or as a function:

function another_round5(\$x)
{
if( !(\$x % 5) )
return(\$x);
else
return((intval(\$x/5)+1)*5);
}

## Re: Number question

Thanks all but i went with this:
\$round_x = 5 * ceil(\$x/5);

## Re: Number question

Your code gives 10 as output for 6 input, where nearest 5
should be 6.

Try this:

\$round_x = 5 * round( \$x / 5 );

Hilarion

## Re: Number question

Thats what i want it to do:
Round Up
e.g 6 <= 10
therefore 6 == 10

## Re: Number question

\$round_x = 5 * ceil(\$x/5);

or (*maybe* faster):
\$round_x = \$x + ((5 - (\$x%5)) % 5);
--
Simon Stienen <http://dangerouscat.net <http://slashlife.de
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle

Excellent
Thanks for that

## Re: Number question

Craig Keightley wrote:

function round5(\$val) {

\$div=floor((int)\$val/(int)5);
if(\$val%5) {
\$div++;
}

return \$div * 5;
}