Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Best way to round a number.

by BrowserUk (Pope)
on Oct 25, 2012 at 01:01 UTC ( #1000744=note: print w/ replies, xml ) Need Help??


in reply to Best way to round a number.

If you actually want "the nearest multiple of 5", then:

sub roundToNearest5{ 5 * int( ( $_[0] + ( 2 * ( $_[0] <=> 0 ) ) ) / 5 ); } print "$_: ", roundToNearest5( $_ ) for -18..+18;; -18: -20 -17: -15 -16: -15 -15: -15 -14: -15 -13: -15 -12: -10 -11: -10 -10: -10 -9: -10 -8: -10 -7: -5 -6: -5 -5: -5 -4: -5 -3: -5 -2: 0 -1: 0 0: 0 1: 0 2: 0 3: 5 4: 5 5: 5 6: 5 7: 5 8: 10 9: 10 10: 10 11: 10 12: 10 13: 15 14: 15 15: 15 16: 15 17: 15 18: 20

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

codecode
Comment on Re: Best way to round a number.
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1000744]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2016-05-25 02:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?