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


Groups > comp.lang.python > #20156

Re: round down to nearest number

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <alec.taylor6@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'rounds': 0.07; '>>>>': 0.09; 'integers': 0.09; 'rounded': 0.09; 'def': 0.13; 'received:209.85.214.174': 0.13; 'from:addr:alec.taylor6': 0.16; 'from:name:alec taylor': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; "haven't": 0.20; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'correctly.': 0.24; 'url:mailman': 0.27; 'function': 0.27; 'cc:addr:gmail.com': 0.28; 'not.': 0.28; "i'm": 0.28; 'message- id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'kelly': 0.30; 'subject:number': 0.30; '(as': 0.31; 'url:listinfo': 0.32; 'cases': 0.32; 'opposed': 0.32; 'does': 0.32; 'fri,': 0.34; 'round': 0.34; 'surprised': 0.34; 'operations': 0.35; '...': 0.35; 'url:python': 0.35; 'received:209.85.214': 0.36; 'cc:2**1': 0.36; 'two': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.38; 'url:org': 0.39; 'received:209': 0.39; '100': 0.70; 'subject:down': 0.84; 'subject:round': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=p/ZAs59D7XOewN8mfnJttmjBAdj/eh1WvAknB3g7OUs=; b=pJCbW1fxjqT1yzBVVp5RzTaocValgqjJJ3x/sqYV93n/IdekG+HdPGw/KskzNr6DGJ zNiooxzgHPIocg+XmD0FESXg3jyaGA763CqkUZq8+gA/+rYsOwsB4TBujtJ+1+QwdKSx F7uza+vJsBn5ILAKdbPlYaIrUcEvHfjf9xG18=
MIME-Version 1.0
In-Reply-To <CAJ6cK1YzuDkMr3ha0E4giV+RtM0dCt_EeX56ipcQiqP6CGO9-A@mail.gmail.com>
References <460b3d7b-d2ce-492a-ab61-ea4a1ee58198@1g2000yqv.googlegroups.com> <mailman.5623.1328834861.27778.python-list@python.org> <d7e99230-5687-4407-b998-a59cffe9833a@i18g2000yqf.googlegroups.com> <CAMZYqRQTPH-NC-9pL=sF0q13VYiwe-BjhPusQ_aYnaA-GcnV3A@mail.gmail.com> <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 Fri, 10 Feb 2012 22:05:39 +1100
Subject Re: round down to nearest number
From Alec Taylor <alec.taylor6@gmail.com>
To Arnaud Delobelle <arnodel@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5651.1328871943.27778.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328871943 news.xs4all.nl 6850 [2001:888:2000:d::a6]:56643
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20156

Show key headers only | 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