455,446 Members | 1,556 Online
Need help? Post your question and get tips & solutions from a community of 455,446 IT Pros & Developers. It's quick & easy.

# Number question

 P: n/a 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 Replies

 P: n/a 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 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 »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

 P: n/a Excellent Thanks for that "Simon Stienen" wrote in message news:zz**************@news.dangerouscat.net... 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 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 »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

 P: n/a 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

 P: n/a 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" 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

 P: n/a Or as a function: function another_round5(\$x) { if( !(\$x % 5) ) return(\$x); else return((intval(\$x/5)+1)*5); } Ehtor 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" 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

 P: n/a Thanks all but i went with this: \$round_x = 5 * ceil(\$x/5); "Ehtor" 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 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" 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

 P: n/a > \$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

 P: n/a "Craig Keightley" 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

 P: n/a Thats what i want it to do: Round Up e.g 6 <= 10 therefore 6 == 10 "Hilarion" 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 discussion thread is closed

Replies have been disabled for this discussion.