Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Rounding to the nearest 50

by Crian (Curate)
on Mar 01, 2004 at 14:51 UTC ( [id://332930]=note: print w/replies, xml ) Need Help??


in reply to Rounding to the nearest 50

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://332930]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found