### Re: Best way to round a number.

by Perlbotics (Chancellor)
 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

Create A New User
Node Status?
node history
Node Type: note [id://1001137]
help
Chatterbox?
 [LanX]: (they are pronounced differently) [choroba]: perl -Mutf8 -Mfeature=fc -wE 'say fc "straße" eq fc "strasse"' [choroba]: returns 1 for me [choroba]: are they? [LanX]: strict rules : the a is long in "straße" and short in "strasse" [choroba]: So the "a" is different, not the "s" ;-) [LanX]: people knowing that it's a workaround will pronounce it right [LanX]: well [LanX]: Maße and Masse are two very different words. LanX (measures/mass)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2018-03-21 15:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (268 votes). Check out past polls.

Notices?