Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Rounding up to the next .5

Reply
Thread Tools

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?

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.

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
 
 
 
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)
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.

--
Justin C, by the sea.
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
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
 
Reply With Quote
 
Tad McClellan
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)
> 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


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
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)
>> 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


Thanks. I understand most of what that document says, the notation does
my head in though

Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Rounding up to the next 100 Michael.Coll-Barth@VerizonWireless.com Python 5 01-22-2010 05:02 AM
Rounding up to the next 100 noydb Python 4 01-21-2010 10:03 PM
Next (the other next) Gen "DVD" storage Alan Browne Digital Photography 13 05-31-2005 05:53 PM
CurrentElement->next = CurrentElement->next->next (UNDEFINED?) Deniz Bahar C Programming 2 03-09-2005 12:45 AM
Rounding to next highest number? NoKetch C Programming 7 12-15-2003 11:23 PM



Advertisments