473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Number question

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
Jul 17 '05 #1
9 1811
Craig Keightley <do**@spam.me> 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

Many Thanks for any help provide
Craig


$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
Jul 17 '05 #2
Excellent
Thanks for that
"Simon Stienen" <si***********@news.slashlife.de> wrote in message
news:zz**************@news.dangerouscat.net...
Craig Keightley <do**@spam.me> 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

Many Thanks for any help provide
Craig


$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

Jul 17 '05 #3
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;
}
Jul 17 '05 #4
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" <do**@spam.me> wrote in
news:41***********************@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


Jul 17 '05 #5
Or as a function:

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

Ehtor <no****@nomail.com> wrote in
news:Xn*****************************@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" <do**@spam.me> wrote in
news:41***********************@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



Jul 17 '05 #6
Thanks all but i went with this:
$round_x = 5 * ceil($x/5);
"Ehtor" <no****@nomail.com> wrote in message
news:Xn*****************************@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 <no****@nomail.com> wrote in
news:Xn*****************************@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" <do**@spam.me> wrote in
news:41***********************@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


Jul 17 '05 #7
> $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
Jul 17 '05 #8
"Craig Keightley" <do**@spam.me> wrote in message
news:<41***********************@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
Jul 17 '05 #9
Thats what i want it to do:
Round Up
e.g 6 <= 10
therefore 6 == 10

"Hilarion" <hi******@SPAM.op.SMIECI.pl> wrote in message
news:cj**********@news.onet.pl...
$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

Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
3
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record...
8
by: ben | last post by:
hello, i'm trying to answer a "prove an upper bound on the number of machine instructions required to process M connections on N objects" exercise from an algorithms book. i don't understand a...
4
by: sathyashrayan | last post by:
(This is not a home work question) Dear group, I want a program to find one number between a set of natural number.A program to guess a number in between a Natural number set.This should be a...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
13
by: S.Dickson | last post by:
I had an access database that i use as an ordering system. I have a form for entering customer details. When i add a new customer on the form the customer number is an auto number that appears when...
22
by: gagan.singh.arora | last post by:
Hi there. I want to generate random numbers with a given probability, say 80% even and 20% odd. Is it possible to implement such an algorithm in C?
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
13
by: Rajshekhar | last post by:
Hi Group, I woud like to know how can i store a number generated out of 2 to power of 150 i.e pow(2,150); mainly i want to knw how can i get the value a large powers of a number....! ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.