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


Groups > comp.lang.python > #20142

Re: round down to nearest number

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.019
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'mrab': 0.04; '>>>>': 0.09; 'received:mail-lpp01m010-f46.google.com': 0.09; 'rounded': 0.09; 'rounding': 0.09; 'digits.': 0.16; 'doesnt': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'cheers,': 0.20; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'correctly.': 0.24; 'not.': 0.28; 'message- id:@mail.gmail.com': 0.29; 'pm,': 0.29; 'down,': 0.30; 'subject:number': 0.30; 'received:209.85.215.46': 0.32; 'thu,': 0.32; 'does': 0.32; 'round': 0.34; 'to:addr:python-list': 0.35; '(for': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.38; 'received:209.85.215': 0.39; 'received:209': 0.39; 'to:addr:python.org': 0.40; 'more': 0.61; 'alternative': 0.65; '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:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=HlYu4FVd4j0RgF4w7Q9eznfd8XOVHaOo90TK6PfYT7s=; b=bP3UjFfGIEXC4KpWCuLSIW7f8/QQE899zVQ5QyZoUK/zdLxKkQiDsfUumAygfQlm/A N9rlqRUotEmibpCO9t8oS0TLw1HRuobtQmqThhUgOAyNOBtLrHFgcHnIJRs0eY8whaoc y1fOpvjwwZiEgj8dCOe47z+GTrkLwHtIpcOZA=
MIME-Version 1.0
In-Reply-To <4F3490AB.7080701@mrabarnett.plus.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>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 9 Feb 2012 23:21:31 -0700
Subject Re: round down to nearest number
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
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.5639.1328854923.27778.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328854923 news.xs4all.nl 6908 [2001:888:2000:d::a6]:33229
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20142

Show key headers only | View raw


On Thu, Feb 9, 2012 at 8:36 PM, MRAB <python@mrabarnett.plus.com> 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

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