C.R. wrote:
Does anyone have a routine that will round an integer to a multiple of 5?

Didn't find anything on CPAN? Odd...

A search for "Round" -> http://search.cpan.org/search?query=Round&mode=all

Yeilded Math::Round, nearest() looks promising.

For example: if number ends in 0 or 5, no rounding is done. If number ends in 1,2 the ones place rounds down to 0. If number ends in 3,4 the ones place rounds up to 5. If number ends in 6,7 the ones place rounds down to 5. If number ends in 8,9 the ones places rounds up to 0 and tens places goes up by 1. Examples: Number Rounded
1628     1630
1625     1625
1621     1620
1610     1610

Thanks!

You've already written the logic :) Now just change it from english to Perl, say in a nice fast easy to follow and maintain ternary table like so:

sub round_to_nearest_five {
    my $integer = int(shift);

    my $last = substr($integer, length($integer) - 1, 1);

    $integer += $last == 1 || $last == 6 ? -1
              : $last == 2 || $last == 7 ? -2
              : $last == 3 || $last == 8 ? 2
              : $last == 4 || $last == 9 ? 1
              :                            0
              ;

    return $integer;
}

But use the module, its more flexible, reusable, consistent, in depth, etc etc :)

HTH!

--
Dan Muey

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to