in reply to Rounding values upwards on an arbitrary basis

#!/usr/bin/perl use strict; use warnings; use POSIX qw /ceil/; my @data = qw /1 2 4 5 9 10 99 100 110 124 125 900 999 1000 1100/; foreach (@data) { my $frac = $_ < 100 ? 5 : (1 . ("0" x (length ($_) - 1))) / 4; my $new = $frac * ceil ($_ / $frac); print "$_: $new\n"; } __END__ 1: 5 2: 5 4: 5 5: 5 9: 10 10: 10 99: 100 100: 100 110: 125 124: 125 125: 125 900: 900 999: 1000 1000: 1000 1100: 1250

Abigail

Replies are listed 'Best First'.
Re: Re: Rounding values upwards on an arbitrary basis
by Thelonius (Priest) on Jul 02, 2003 at 21:03 UTC
    Well, the bizarre thing about this problem is that the function ibanix described is not idempotent. E.g. f(124)=125 but f(f(124))=150. So it shouldn't really be called a "rounding" function. I don't know if that's really what he wanted, but it is what he asked for.

    Abigail's function is more reasonable, but not what ibanix asked for.

      Yeah, well you could solve that by adding something like:
      $new += $frac if int ($_ / $frac) == ceil ($_ / $frac);

      Abigail