Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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 pondering the Monastery: (7)
As of 2014-08-22 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (145 votes), past polls