Discussion:
round up to nearest...
Bryan Harris
2005-08-20 00:01:13 UTC
Permalink
Not exactly perl, but ...

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

I remember seeing another formula for rounding a value X to the nearest
multiple of T -- I'd love that one too, if someone has a list of handy
formulas.

Or is there a module perhaps that already does this?

- B
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
zentara
2005-08-20 13:13:22 UTC
Permalink
Post by Bryan Harris
Not exactly perl, but ...
Is there a simple formula to round some value X up to the next multiple of
some other value T?
I remember seeing another formula for rounding a value X to the nearest
multiple of T -- I'd love that one too, if someone has a list of handy
formulas.
Or is there a module perhaps that already does this?
use Math::Round ??

perl -MMath::Round -e'print nearest(.01, 1.555)'
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Jeff 'japhy' Pinyan
2005-08-21 17:17:10 UTC
Permalink
Post by Bryan Harris
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: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Bryan Harris
2005-08-21 20:27:37 UTC
Permalink
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
Post by Jeff 'japhy' Pinyan
Post by Bryan Harris
Is there a simple formula to round some value X up to the next multiple of
some other value T?
$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.
$x - ($x % $t)
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Rex Rex
2005-08-22 11:24:34 UTC
Permalink
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
Post by Bryan Harris
Neat, I like it!
Is this the best way to do simple integer round-ups? E.g. 3.2 -> 4?
$nines = 1 - 1/1e10;
$value = 3.2;
$roundedvalue = int($value + $nines);
... but it looks like $roundedvalue = $value + (-$value % 1) might be
better???
- B
Post by Jeff 'japhy' Pinyan
Post by Bryan Harris
Is there a simple formula to round some value X up to the next multiple of
some other value T?
$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.
$x - ($x % $t)
--
<http://learn.perl.org/> <http://learn.perl.org/first-response>
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Jeff 'japhy' Pinyan
2005-08-22 12:48:51 UTC
Permalink
Post by Bryan Harris
Is this the best way to do simple integer round-ups? E.g. 3.2 -> 4?
$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 showed is only for integers. The generic function for
rounding *any* number to a multiple of 1, 10, 100, (or .1, .01, .001,
etc.) is as follows:

sub round {
my ($n, $places) = @_;
my $factor = 10 ** ($places || 0);
return int(($n * $factor) + ($n < 0 ? -1 : 1) * 0.5) / $factor;
}

To round a number to the nearest integer, use round($x). To round it to
the nearest 10, use round($x, 1). Nearest 100 is round($x, 2) and so on.
You can also round to the nearest tenth, hundredth, etc., by using a
negative second argument: round(5.28, -1) == 5.3.

If you always want to round a number up to the next integer value (unless
the value is an integer), use ceil() from the POSIX module. ceil(1.1) ==
2, ceil(1.9) == 2, ceil(2) == 2. To always round down, use floor() from
POSIX.
--
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: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Continue reading on narkive:
Loading...