First of all, you are not looking for the nearest value. What you need is a customised ceil (see perldoc POSIX).

In such cases, where you want to look for the "nearest but not smaller then" and your step is not 1, but X (X = 50 in your case), you can do the following:

use POSIX; sub ceilX ($$); print "$_ -> ", ceilX($_, 50), "\n" for (145..154); sub ceilX ($$) { my $number = shift; my $x = shift; return POSIX::ceil($number / $x) * $x; }


with the result

145 -> 150 146 -> 150 147 -> 150 148 -> 150 149 -> 150 150 -> 150 151 -> 200 152 -> 200 153 -> 200 154 -> 200


Edit: Sorry, I was interrupted while writing this answer, when I started writing, there was no comparable posted.

In reply to Re: Rounding to the nearest 50 by Crian
in thread Rounding to the nearest 50 by Hammy

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.