round down to nearest number

Discussion in 'Python' started by noydb, Feb 10, 2012.

  1. noydb

    noydb Guest

    How do you round down ALWAYS to nearest 100? Like, if I have number
    3268, I want that rounded down to 3200. I'm doing my rounding like
    >>> round(3268, -2)

    But, how to round DOWN?
     
    noydb, Feb 10, 2012
    #1
    1. Advertising

  2. noydb

    Ian Kelly Guest

    On Thu, Feb 9, 2012 at 5:30 PM, noydb <> wrote:
    > How do you round down ALWAYS to nearest 100?  Like, if I have number
    > 3268, I want that rounded down to 3200.  I'm doing my rounding like
    >>>> round(3268, -2)

    > But, how to round DOWN?


    >>> 3268 // 100 * 100

    3200

    For more complicated cases, Decimal objects allow you to specify
    alternate rounding modes.
     
    Ian Kelly, Feb 10, 2012
    #2
    1. Advertising

  3. noydb

    noydb Guest

    hmmm, okay.

    So how would you round UP always? Say the number is 3219, so you want
    3300 returned.
     
    noydb, Feb 10, 2012
    #3
  4. noydb

    Chris Rebert Guest

    Chris Rebert, Feb 10, 2012
    #4
  5. noydb

    Ian Kelly Guest

    On Thu, Feb 9, 2012 at 6:43 PM, Chris Rebert <> wrote:
    > On Thu, Feb 9, 2012 at 5:23 PM, noydb <> wrote:
    >> hmmm, okay.
    >>
    >> So how would you round UP always?  Say the number is 3219, so you want
    >> 3300 returned.

    >
    > http://stackoverflow.com/questions/17944/how-to-round-up-the-result-of-integer-division/96921
    >
    > Thus: (3219 + 99) // 100
    >
    > Slight tangent: Beware negative numbers when using // or %.


    There's no problem with negative numbers here, as long as you actually
    want to round *up* or *down*, as opposed to away from zero or toward
    zero.
     
    Ian Kelly, Feb 10, 2012
    #5
  6. noydb

    noydb Guest

    That {>>> (3219 + 99) // 100} doesnt work if the number is other then
    4 digits.


    (for rounding up to nearest 100):
    >>> (3219 + 99)//100

    33
    >>> (3289 + 99)//100

    33
    >>> (328678 + 99)//100

    3287
    >>> (328 + 99)//100

    4
     
    noydb, Feb 10, 2012
    #6
  7. noydb

    Terry Reedy Guest

    On 2/9/2012 8:23 PM, noydb wrote:
    > So how would you round UP always? Say the number is 3219, so you want
    >>> (3333//100+1)*100

    3400

    --
    Terry Jan Reedy
     
    Terry Reedy, Feb 10, 2012
    #7
  8. noydb

    MRAB Guest

    On 10/02/2012 02:25, noydb wrote:
    > That {>>> (3219 + 99) // 100} doesnt work if the number is other then
    > 4 digits.
    >
    >
    > (for rounding up to nearest 100):
    >>>> (3219 + 99)//100

    > 33
    >>>> (3289 + 99)//100

    > 33
    >>>> (328678 + 99)//100

    > 3287
    >>>> (328 + 99)//100

    > 4


    >>> (3219 + 99) // 100 * 100

    3300
    >>> (3289 + 99) // 100 * 100

    3300
    >>> (328678 + 99) // 100 * 100

    328700
    >>> (328 + 99) // 100 * 100

    400

    Those are all rounded up to the nearest 100 correctly.
     
    MRAB, Feb 10, 2012
    #8
  9. noydb

    MRAB Guest

    On 10/02/2012 03:29, Terry Reedy wrote:
    > On 2/9/2012 8:23 PM, noydb wrote:
    >> So how would you round UP always? Say the number is 3219, so you want
    > >>> (3333//100+1)*100

    > 3400
    >

    Doing it that way doesn't always work. For example:

    >>> (3400 // 100 + 1) * 100

    3500

    However:

    >>> (3400 + 99) // 100 * 100

    3400
     
    MRAB, Feb 10, 2012
    #9
  10. noydb

    Ian Kelly Guest

    On Thu, Feb 9, 2012 at 8:36 PM, MRAB <> wrote:
    > On 10/02/2012 02:25, noydb wrote:
    >>
    >> That {>>>  (3219 + 99) // 100} doesnt work if the number is other then
    >> 4 digits.
    >>
    >>
    >> (for rounding up to nearest 100):
    >>>>>
    >>>>>  (3219 + 99)//100

    >>
    >> 33
    >>>>>
    >>>>>  (3289 + 99)//100

    >>
    >> 33
    >>>>>
    >>>>>  (328678 + 99)//100

    >>
    >> 3287
    >>>>>
    >>>>>  (328 + 99)//100

    >>
    >> 4

    >
    >
    >>>> (3219 + 99) // 100 * 100

    > 3300
    >>>> (3289 + 99) // 100 * 100

    > 3300
    >>>> (328678 + 99) // 100 * 100

    > 328700
    >>>> (328 + 99) // 100 * 100

    > 400
    >
    > Those are all rounded up to the nearest 100 correctly.


    One thing to be aware of though is that while the "round down" formula
    works interchangeably for ints and floats, the "round up" formula does
    not.

    >>> (3300.5 + 99) // 100 * 100

    3300.0

    A more consistent alternative is to negate the number, round down, and
    then negate again.

    >>> -(-(3300.5) // 100 * 100)

    3400.0

    Cheers,
    Ian
     
    Ian Kelly, Feb 10, 2012
    #10
  11. On 10 February 2012 06:21, Ian Kelly <> wrote:
    >>>>> (3219 + 99) // 100 * 100

    >> 3300
    >>>>> (3289 + 99) // 100 * 100

    >> 3300
    >>>>> (328678 + 99) // 100 * 100

    >> 328700
    >>>>> (328 + 99) // 100 * 100

    >> 400
    >>
    >> Those are all rounded up to the nearest 100 correctly.

    >
    > One thing to be aware of though is that while the "round down" formula
    > works interchangeably for ints and floats, the "round up" formula does
    > not.
    >
    >>>> (3300.5 + 99) // 100 * 100

    > 3300.0
    >


    I'm surprised I haven't seen:

    >>> 212 - (212 % -100)

    300

    Here's a function that:
    * rounds up and down
    * works for both integers and floats
    * is only two operations (as opposed to 3 in the solutions given above)

    >>> def round(n, k):

    .... return n - n%k
    ....
    >>> # Round down with a positive k:

    .... round(167, 100)
    100
    >>> round(-233, 100

    .... )
    -300
    >>> # Round up with a negative k:

    .... round(167, -100)
    200
    >>> round(-233, -100)

    -200
    >>> # Edge cases

    .... round(500, -100)
    500
    >>> round(500, 100)

    500
    >>> # Floats

    .... round(100.5, -100)
    200.0
    >>> round(199.5, 100)

    100.0

    --
    Arnaud
     
    Arnaud Delobelle, Feb 10, 2012
    #11
  12. noydb

    Alec Taylor Guest

    o_O

    Very nice

    On Fri, Feb 10, 2012 at 8:58 PM, Arnaud Delobelle <> wrote:
    > On 10 February 2012 06:21, Ian Kelly <> wrote:
    >>>>>> (3219 + 99) // 100 * 100
    >>> 3300
    >>>>>> (3289 + 99) // 100 * 100
    >>> 3300
    >>>>>> (328678 + 99) // 100 * 100
    >>> 328700
    >>>>>> (328 + 99) // 100 * 100
    >>> 400
    >>>
    >>> Those are all rounded up to the nearest 100 correctly.

    >>
    >> One thing to be aware of though is that while the "round down" formula
    >> works interchangeably for ints and floats, the "round up" formula does
    >> not.
    >>
    >>>>> (3300.5 + 99) // 100 * 100

    >> 3300.0
    >>

    >
    > I'm surprised I haven't seen:
    >
    >>>> 212 - (212 % -100)

    > 300
    >
    > Here's a function that:
    > * rounds up and down
    > * works for both integers and floats
    > * is only two operations (as opposed to 3 in the solutions given above)
    >
    >>>> def round(n, k):

    > ...     return n - n%k
    > ...
    >>>> # Round down with a positive k:

    > ... round(167, 100)
    > 100
    >>>> round(-233, 100

    > ... )
    > -300
    >>>> # Round up with a negative k:

    > ... round(167, -100)
    > 200
    >>>> round(-233, -100)

    > -200
    >>>> # Edge cases

    > ... round(500, -100)
    > 500
    >>>> round(500, 100)

    > 500
    >>>> # Floats

    > ... round(100.5, -100)
    > 200.0
    >>>> round(199.5, 100)

    > 100.0
    >
    > --
    > Arnaud
    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Alec Taylor, Feb 10, 2012
    #12
  13. noydb

    noydb Guest

    On Feb 10, 4:58 am, Arnaud Delobelle <> wrote:
    > On 10 February 2012 06:21, Ian Kelly <> wrote:
    >
    >
    >
    >
    >
    > >>>>> (3219 + 99) // 100 * 100
    > >> 3300
    > >>>>> (3289 + 99) // 100 * 100
    > >> 3300
    > >>>>> (328678 + 99) // 100 * 100
    > >> 328700
    > >>>>> (328 + 99) // 100 * 100
    > >> 400

    >
    > >> Those are all rounded up to the nearest 100 correctly.

    >
    > > One thing to be aware of though is that while the "round down" formula
    > > works interchangeably for ints and floats, the "round up" formula does
    > > not.

    >
    > >>>> (3300.5 + 99) // 100 * 100

    > > 3300.0

    >
    > I'm surprised I haven't seen:
    >
    > >>> 212 - (212 % -100)

    >
    > 300
    >
    > Here's a function that:
    > * rounds up and down
    > * works for both integers and floats
    > * is only two operations (as opposed to 3 in the solutions given above)
    >
    > >>> def round(n, k):

    >
    > ...     return n - n%k
    > ...>>> # Round down with a positive k:
    >
    > ... round(167, 100)
    > 100>>> round(-233, 100
    >
    > ... )
    > -300>>> # Round up with a negative k:
    >
    > ... round(167, -100)
    > 200>>> round(-233, -100)
    > -200
    > >>> # Edge cases

    >
    > ... round(500, -100)
    > 500>>> round(500, 100)
    > 500
    > >>> # Floats

    >
    > ... round(100.5, -100)
    > 200.0>>> round(199.5, 100)
    >
    > 100.0
    >
    > --
    > Arnaud- Hide quoted text -
    >
    > - Show quoted text -


    Thanks! Covers all bases, good.
     
    noydb, Feb 10, 2012
    #13
  14. noydb

    Olive Guest

    On Thu, 9 Feb 2012 17:43:58 -0800
    Chris Rebert <> wrote:

    > On Thu, Feb 9, 2012 at 5:23 PM, noydb <> wrote:
    > > hmmm, okay.
    > >
    > > So how would you round UP always?  Say the number is 3219, so you
    > > want 3300 returned.

    >
    > http://stackoverflow.com/questions/17944/how-to-round-up-the-result-of-integer-division/96921
    >
    > Thus: (3219 + 99) // 100
    >
    > Slight tangent: Beware negative numbers when using // or %.


    This trick work always (even if the entry is a float):


    -(-a//100)*100

    >>> -(-3219//100)*100

    3300

    >>> -(-3200.1//100)*100


    3300.0
     
    Olive, Feb 10, 2012
    #14
  15. Terry Reedy <> writes:

    > On 2/9/2012 8:23 PM, noydb wrote:
    >> So how would you round UP always? Say the number is 3219, so you want
    >>>> (3333//100+1)*100

    > 3400


    Note that that doesn't work for numbers that are already round:

    >>> (3300//100+1)*100

    3400 # 3300 would be correct

    I'd go with Chris Rebert's (x + 99) // 100.
     
    Hrvoje Niksic, Feb 11, 2012
    #15
    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. Steve
    Replies:
    5
    Views:
    42,709
    Steve
    May 17, 2004
  2. Fred
    Replies:
    3
    Views:
    63,539
  3. tertius

    round() to nearest .05 ?

    tertius, Nov 15, 2004, in forum: Python
    Replies:
    4
    Views:
    781
    tertius
    Nov 16, 2004
  4. Don
    Replies:
    30
    Views:
    6,275
    murari garg
    Mar 4, 2007
  5. round to the nearest power of 4

    , Jan 29, 2007, in forum: C Programming
    Replies:
    4
    Views:
    465
    David T. Ashley
    Jan 29, 2007
Loading...

Share This Page