Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Best way to round a number.

by Perlbotics (Canon)
on Oct 26, 2012 at 21:07 UTC ( #1001137=note: print w/ replies, xml ) Need Help??


in reply to Best way to round a number.

use strict; use warnings; sub round5 { my $num = shift; return $num+2 - ($num+2) % 5; # <------ TIMTOWTDI ;-) } for (0..10) { printf "%3d -> %3d %3d -> %3d\n", $_, round5($_), -$_, round5(-$_); } __DATA__ 0 -> 0 0 -> 0 1 -> 0 -1 -> 0 2 -> 0 -2 -> 0 3 -> 5 -3 -> -5 4 -> 5 -4 -> -5 5 -> 5 -5 -> -5 6 -> 5 -6 -> -5 7 -> 5 -7 -> -5 8 -> 10 -8 -> -10 9 -> 10 -9 -> -10 10 -> 10 -10 -> -10


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://1001137]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2015-07-02 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (28 votes), past polls