On Aug 19, Bryan Harris said:

Is there a simple formula to round some value X up to the next multiple of
some other value T?

Generally speaking, you can do:

  $x + (-$x % $t)

For 10 to round up to the next multiple of 3, it's 10 + (-10 % 3) which is 10 + 2 = 12. Likewise, for negative numbers: -14 to round up to the next multiple of 5 is -14 + (14 % 5) which is -14 + 4 = -10.

To round down, it's simply:

  $x - ($x % $t)

--
Jeff "japhy" Pinyan         %  How can we ever be the sold short or
RPI Acacia Brother #734     %  the cheated, we who for every service
http://japhy.perlmonk.org/  %  have long ago been overpaid?
http://www.perlmonks.org/   %    -- Meister Eckhart

--
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