Rounding up to the next .5

Discussion in 'Perl Misc' started by Justin C, Apr 28, 2007.

  1. Justin C

    Justin C Guest

    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.
    Justin C, Apr 28, 2007
    #1
    1. Advertising

  2. 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
    Jürgen Exner, Apr 28, 2007
    #2
    1. Advertising

  3. Justin C

    Justin C Guest

    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.
    Justin C, Apr 28, 2007
    #3
  4. Justin C

    -berlin.de Guest

    Justin C <> 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
    -berlin.de, Apr 28, 2007
    #4
  5. Justin C <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 29, 2007
    #5
  6. Justin C

    Justin C Guest

    In article <>, Tad McClellan wrote:
    > Justin C <> 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.
    Justin C, Apr 29, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. NoKetch

    Rounding to next highest number?

    NoKetch, Dec 15, 2003, in forum: C Programming
    Replies:
    7
    Views:
    571
    Mark McIntyre
    Dec 15, 2003
  2. Deniz Bahar
    Replies:
    2
    Views:
    453
    Andrey Tarasevich
    Mar 9, 2005
  3. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    779
    Alf P. Steinbach
    Jul 10, 2007
  4. noydb

    Rounding up to the next 100

    noydb, Jan 21, 2010, in forum: Python
    Replies:
    4
    Views:
    700
  5. Replies:
    5
    Views:
    341
    casevh
    Jan 22, 2010
Loading...

Share This Page