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: 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> <4F3490AB.7080701@mrabarnett.plus.com> From: Ian Kelly 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Thu, Feb 9, 2012 at 8:36 PM, MRAB wrote: > On 10/02/2012 02:25, noydb wrote: >> >> That {>>> =A0(3219 + 99) // 100} doesnt work if the number is other then >> 4 digits. >> >> >> (for rounding up to nearest 100): >>>>> >>>>> =A0(3219 + 99)//100 >> >> 33 >>>>> >>>>> =A0(3289 + 99)//100 >> >> 33 >>>>> >>>>> =A0(328678 + 99)//100 >> >> 3287 >>>>> >>>>> =A0(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