Rounding up to the next .5

J

Justin C

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?

Thank you for any help you can give.

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.
 
J

Jürgen Exner

Justin said:
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
 
J

Justin C

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)
already installed.

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.
 
A

anno4000

Justin C said:
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
 
T

Tad McClellan

Justin C said:
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)
already installed.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,838
Latest member
KandiceChi

Latest Threads

Top