Re: [quickie] Rounding up to next multiple

Ian Phillipps <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
On Wed, 13 Aug 2003 at 14:46:22 +0100, Paul Makepeace wrote:

> This isn't really perl, but it might be mildly fun for a few seconds:
>   $len += $page_size - $len % $page_size if $len % $page_size;
> Any way of doing this in a single arithmetic expression, without the if?

Assuming integers:

$len = $page_size * ( int( ( $page_size + $len - 1 ) / $page_size ) );

If floating point, then the implicit equality comparison doesn't work,
but you can substitute a very small number instead of 1 to make it mostly
work.

Ian
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.