On Aug 21, Bryan Harris said:
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???
That technique I show
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
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 sai
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 numb