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
In reply to Re: Best way to round a number.
by BrowserUk
in thread Best way to round a number.
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |