Here's an approach to the rounding up function. I've left the algorithm of what to round by at each level as exercise for the reader, as you said you know how using log()

CODE: #!/usr/bin/perl -w use strict; # $n is number to round, $d is "divisor" for rounding units sub roundup { my ($n, $d) = @_; return ( int( $n / $d ) + ( ( $n % $d ) ? 1 : 0 ) ) * $d; } for my $i ( 0,4,5,6,9,10,11,24,25,26) { print "$i -> " . roundup($i,5) . "\n"; } for my $i ( 99,100,101,124,125,126,149,150,151) { print "$i -> " . roundup($i,25) . "\n"; } for my $i ( 999, 1000, 1001, 1249, 1250, 1251, 1499,1500,1501) { print "$i -> " . roundup($i,250) . "\n"; } RESULT: 0 -> 0 4 -> 5 5 -> 5 6 -> 10 9 -> 10 10 -> 10 11 -> 15 24 -> 25 25 -> 25 26 -> 30 99 -> 100 100 -> 100 101 -> 125 124 -> 125 125 -> 125 126 -> 150 149 -> 150 150 -> 150 151 -> 175 999 -> 1000 1000 -> 1000 1001 -> 1250 1249 -> 1250 1250 -> 1250 1251 -> 1500 1499 -> 1500 1500 -> 1500 1501 -> 1750

-xdg

Addendum: This rounds up, but not over -- for the case where 4 rounds up to 5, but 5 rounds up to 10 (!), then the function is even simpler:

sub roundup { my ($n, $d) = @_; return ( int( $n / $d ) + 1 ) * $d; }

Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.


In reply to Re: Rounding values upwards on an arbitrary basis by xdg
in thread Rounding values upwards on an arbitrary basis by ibanix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.