Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20156

Re: round down to nearest number

References (4 earlier) <mailman.5629.1328839269.27778.python-list@python.org> <afdcb636-f098-4de0-9f75-71f372fe896c@k40g2000yqf.googlegroups.com> <4F3490AB.7080701@mrabarnett.plus.com> <CALwzidnD4tUtaQnTakjZw2M7=ZUB6KYLwYaTHE9H3+X5vYJ7NA@mail.gmail.com> <CAJ6cK1YzuDkMr3ha0E4giV+RtM0dCt_EeX56ipcQiqP6CGO9-A@mail.gmail.com>
Date 2012-02-10 22:05 +1100
Subject Re: round down to nearest number
From Alec Taylor <alec.taylor6@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.5651.1328871943.27778.python-list@python.org> (permalink)

Show all headers | View raw


o.O

Very nice

On Fri, Feb 10, 2012 at 8:58 PM, Arnaud Delobelle <arnodel@gmail.com> wrote:
> On 10 February 2012 06:21, Ian Kelly <ian.g.kelly@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

round down to nearest number noydb <jenn.duerr@gmail.com> - 2012-02-09 16:30 -0800
  Re: round down to nearest number Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-09 17:47 -0700
    Re: round down to nearest number noydb <jenn.duerr@gmail.com> - 2012-02-09 17:23 -0800
      Re: round down to nearest number Chris Rebert <clp2@rebertia.com> - 2012-02-09 17:43 -0800
        Re: round down to nearest number Olive <diolu@bigfoot.com> - 2012-02-10 21:56 +0100
      Re: round down to nearest number Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-09 19:00 -0700
        Re: round down to nearest number noydb <jenn.duerr@gmail.com> - 2012-02-09 18:25 -0800
          Re: round down to nearest number MRAB <python@mrabarnett.plus.com> - 2012-02-10 03:36 +0000
          Re: round down to nearest number Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-09 23:21 -0700
          Re: round down to nearest number Arnaud Delobelle <arnodel@gmail.com> - 2012-02-10 09:58 +0000
            Re: round down to nearest number noydb <jenn.duerr@gmail.com> - 2012-02-10 09:23 -0800
          Re: round down to nearest number Alec Taylor <alec.taylor6@gmail.com> - 2012-02-10 22:05 +1100
      Re: round down to nearest number Terry Reedy <tjreedy@udel.edu> - 2012-02-09 22:29 -0500
        Re: round down to nearest number Hrvoje Niksic <hniksic@xemacs.org> - 2012-02-11 11:26 +0100
      Re: round down to nearest number MRAB <python@mrabarnett.plus.com> - 2012-02-10 03:39 +0000

csiph-web