For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > round up to nearest...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author round up to nearest...
Bryan Harris

2005-08-20, 7:56 am



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


Zentara

2005-08-20, 6:56 pm

On Fri, 19 Aug 2005 17:01:13 -0700, lists@harrisfam.net (Bryan Harris)
wrote:

>
>
>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
Jeff 'japhy' Pinyan

2005-08-21, 6:55 pm

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)

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

2005-08-22, 7:55 am



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



Rex Rex

2005-08-22, 7:55 am

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 =3D 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 <lists@harrisfam.net> wrote:
>=20
>=20
> Neat, I like it!
>=20
> Is this the best way to do simple integer round-ups? E.g. 3.2 -> 4?
>=20
> I've been using:
>=20
> $nines =3D 1 - 1/1e10;
> $value =3D 3.2;
> $roundedvalue =3D int($value + $nines);
>=20
> ... but it looks like $roundedvalue =3D $value + (-$value % 1) might be
> better???
>=20
> - B
>=20
>=20
e of[color=darkred]
is[color=darkred]
next[color=darkred]
>=20
>=20
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
>=20
>

Jeff 'japhy' Pinyan

2005-08-22, 6:58 pm

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







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com