For ordinary integers, if the rounding up is truly a "rounding" and
not ceiling up to the nearest highest integer, then this should work:

$n = 3.2;
print sprintf("%d", $n); #this will print 3

To ceil the number, wherein 3.2 --> 4 then this would work.

use POSIX;
print ceil($n); # this will print 4.

Cheers,
Rex

On 8/21/05, Bryan Harris <[EMAIL PROTECTED]> wrote:
> 
> 
> Neat, I like it!
> 
> Is this the best way to do simple integer round-ups?  E.g. 3.2 -> 4?
> 
> I've been using:
> 
> $nines = 1 - 1/1e10;
> $value = 3.2;
> $roundedvalue = int($value + $nines);
> 
> ...  but it looks like $roundedvalue = $value + (-$value % 1) might be
> better???
> 
> - B
> 
> 
> > 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)
> 
> 
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
> 
> 
>

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