Velocity Reviews > Perl > Rounding up to the next .5

# Rounding up to the next .5

Justin C
Guest
Posts: n/a

 04-28-2007

Not strictly a perl problem, I'm just happen to be using perl to do
this.

I'm writing a utility for work that will calculate the price to send a
parcel. The courier charges by the half kilo and they never round down.

I'm trying to round weights (with up to two decimal places) up to the
next half kilo (unless the weight is an exact or .5 kilo already).

I've looked at the 'round' in the docs. You can round, for example, four
decimal places to three, two, one, or none, but there's no mention of
rounding, say, .3 to .5.

Things look OK with:

\$weight = ( int( \$kilos + .4 ) *2 ) / 2 ;

Those work until you go to two decimal places, which our scales do
(there is also something called dimensional weight - if your parcel
takes up too much space for how much it weighs, they give it a weight
appropriate for it's size and charge you that instead - we often have to
go to two decimal places).

I've got close with this:

\$weight = ( int( \$kilos + .455 ) * 2 ) / 2 ;

But it's not right, the first fifty grammes over the kilo/half-kilo get
rounded down.

Any suggestions on how I might tackle this?

One last thought, I could use a spreadsheet to create one huge hash,
with all the weights from .01kg up to, say, 200kg. The weights being the
keys and the values being the rounded weight... but there *has* to be a
better way... doesn't there?

Justin.

--
Justin C, by the sea.

Jürgen Exner
Guest
Posts: n/a

 04-28-2007
Justin C wrote:
> I'm writing a utility for work that will calculate the price to send a
> parcel. The courier charges by the half kilo and they never round
> down.
>
> I'm trying to round weights (with up to two decimal places) up to the
> next half kilo (unless the weight is an exact or .5 kilo already).

I think you are making this waaaay to complicated.
For one I am pretty sure there are modules on CPAN that do floor() and
ceiling() functions.
And second there are really only three simple cases that you need to
distinguish:

if the decimal portion is 0 then the shipping weight is the actual weight
If 0 < decimal portion <= 0.5 then the shipping weight is the integer part
plus 0.5
If 0.5 < decimal portion then the shipping weight is the integer part plus 1

jue

Justin C
Guest
Posts: n/a

 04-28-2007
In article <AIKYh.2898\$r77.978@trndny08>, Jürgen Exner wrote:
> Justin C wrote:
>> I'm writing a utility for work that will calculate the price to send a
>> parcel. The courier charges by the half kilo and they never round
>> down.
>>
>> I'm trying to round weights (with up to two decimal places) up to the
>> next half kilo (unless the weight is an exact or .5 kilo already).

>
> I think you are making this waaaay to complicated.

Sometimes you're too busy following logic to see the lateral.

> For one I am pretty sure there are modules on CPAN that do floor() and
> ceiling() functions.

There are that, and, IIRC (from today's Googling for a round() function)
they are part of POSIX, and therefore (if my understanding is correct)

Trouble is, I don't know what those are/do, perldoc wasn't much help,
I suppose they're mathematical functions, I didn't get that far with my
education and so didn't understand, and perldoc -q round, though it
mentioned those, didn't explain them in a way I'd understand that they
are relevant to my problem.

> And second there are really only three simple cases that you need to
> distinguish:
>
> if the decimal portion is 0 then the shipping weight is the actual weight
> If 0 < decimal portion <= 0.5 then the shipping weight is the integer part
> plus 0.5
> If 0.5 < decimal portion then the shipping weight is the integer part plus 1

Now that's a lateral solution to the problem. I'm pretty sure I can
manage that too!

JŸrgen, thank you for your help.

Justin.

--
Justin C, by the sea.

Guest
Posts: n/a

 04-28-2007
Justin C <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> Not strictly a perl problem, I'm just happen to be using perl to do
> this.
>
> I'm writing a utility for work that will calculate the price to send a
> parcel. The courier charges by the half kilo and they never round down.
>
> I'm trying to round weights (with up to two decimal places) up to the
> next half kilo (unless the weight is an exact or .5 kilo already).
>
> I've looked at the 'round' in the docs. You can round, for example, four
> decimal places to three, two, one, or none, but there's no mention of
> rounding, say, .3 to .5.

You can base one on the other. Rounding to the nearest half can be
seen as multiplying by two, then rounding to the nearest integer,
then dividing by two again. Generalizing, you get

sub round_to_multiple {
my ( \$x, \$factor) = @_;
\$factor*sprintf '%.0f', \$x/\$factor;
}

which can be used as

for ( map 0.1*\$_, 0 .. 10 ) {
printf "%s -> %s\n", \$_, round_to_multiple( \$_, 1/2);
}

Anno

Guest
Posts: n/a

 04-29-2007
Justin C <(E-Mail Removed)> wrote:
> In article <AIKYh.2898\$r77.978@trndny08>, Jürgen Exner wrote:
>> Justin C wrote:

>>> I'm trying to round weights (with up to two decimal places) up to the
>>> next half kilo (unless the weight is an exact or .5 kilo already).

>> For one I am pretty sure there are modules on CPAN that do floor() and
>> ceiling() functions.

>
> There are that, and, IIRC (from today's Googling for a round() function)
> they are part of POSIX, and therefore (if my understanding is correct)
>
> Trouble is, I don't know what those are/do, perldoc wasn't much help,
> I suppose they're mathematical functions, I didn't get that far with my
> education and so didn't understand,

http://en.wikipedia.org/wiki/Ceiling_function

--
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas

Justin C
Guest
Posts: n/a

 04-29-2007
In article <(E-Mail Removed)>, Tad McClellan wrote:
> Justin C <(E-Mail Removed)> wrote:
>> In article <AIKYh.2898\$r77.978@trndny08>, Jürgen Exner wrote:
>>> Justin C wrote:

>
>>>> I'm trying to round weights (with up to two decimal places) up to the
>>>> next half kilo (unless the weight is an exact or .5 kilo already).

>
>>> For one I am pretty sure there are modules on CPAN that do floor() and
>>> ceiling() functions.

>>
>> There are that, and, IIRC (from today's Googling for a round() function)
>> they are part of POSIX, and therefore (if my understanding is correct)
>>
>> Trouble is, I don't know what those are/do, perldoc wasn't much help,
>> I suppose they're mathematical functions, I didn't get that far with my
>> education and so didn't understand,

>
> http://en.wikipedia.org/wiki/Ceiling_function

Thanks. I understand most of what that document says, the notation does

Justin.

--
Justin C, by the sea.