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>


Reply via email to