Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20123 > unrolled thread
| Started by | noydb <jenn.duerr@gmail.com> |
|---|---|
| First post | 2012-02-09 16:30 -0800 |
| Last post | 2012-02-10 03:39 +0000 |
| Articles | 15 — 9 participants |
Back to article view | Back to comp.lang.python
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
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2012-02-09 16:30 -0800 |
| Subject | round down to nearest number |
| Message-ID | <460b3d7b-d2ce-492a-ab61-ea4a1ee58198@1g2000yqv.googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-02-09 17:47 -0700 |
| Message-ID | <mailman.5623.1328834861.27778.python-list@python.org> |
| In reply to | #20123 |
On Thu, Feb 9, 2012 at 5:30 PM, noydb <jenn.duerr@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2012-02-09 17:23 -0800 |
| Message-ID | <d7e99230-5687-4407-b998-a59cffe9833a@i18g2000yqf.googlegroups.com> |
| In reply to | #20124 |
hmmm, okay. So how would you round UP always? Say the number is 3219, so you want 3300 returned.
[toc] | [prev] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2012-02-09 17:43 -0800 |
| Message-ID | <mailman.5628.1328838241.27778.python-list@python.org> |
| In reply to | #20127 |
On Thu, Feb 9, 2012 at 5:23 PM, noydb <jenn.duerr@gmail.com> 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 %. Cheers, Chris
[toc] | [prev] | [next] | [standalone]
| From | Olive <diolu@bigfoot.com> |
|---|---|
| Date | 2012-02-10 21:56 +0100 |
| Message-ID | <20120210215644.6d865482@bigfoot.com> |
| In reply to | #20128 |
On Thu, 9 Feb 2012 17:43:58 -0800 Chris Rebert <clp2@rebertia.com> wrote: > On Thu, Feb 9, 2012 at 5:23 PM, noydb <jenn.duerr@gmail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-02-09 19:00 -0700 |
| Message-ID | <mailman.5629.1328839269.27778.python-list@python.org> |
| In reply to | #20127 |
On Thu, Feb 9, 2012 at 6:43 PM, Chris Rebert <clp2@rebertia.com> wrote: > On Thu, Feb 9, 2012 at 5:23 PM, noydb <jenn.duerr@gmail.com> 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.
[toc] | [prev] | [next] | [standalone]
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2012-02-09 18:25 -0800 |
| Message-ID | <afdcb636-f098-4de0-9f75-71f372fe896c@k40g2000yqf.googlegroups.com> |
| In reply to | #20129 |
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
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-02-10 03:36 +0000 |
| Message-ID | <mailman.5636.1328844940.27778.python-list@python.org> |
| In reply to | #20130 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-02-09 23:21 -0700 |
| Message-ID | <mailman.5639.1328854923.27778.python-list@python.org> |
| In reply to | #20130 |
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
[toc] | [prev] | [next] | [standalone]
| From | Arnaud Delobelle <arnodel@gmail.com> |
|---|---|
| Date | 2012-02-10 09:58 +0000 |
| Message-ID | <mailman.5644.1328867924.27778.python-list@python.org> |
| In reply to | #20130 |
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
[toc] | [prev] | [next] | [standalone]
| From | noydb <jenn.duerr@gmail.com> |
|---|---|
| Date | 2012-02-10 09:23 -0800 |
| Message-ID | <f63d9598-bca2-4c4e-b6eb-1da9e60cdb6c@jn12g2000pbb.googlegroups.com> |
| In reply to | #20150 |
On Feb 10, 4:58 am, Arnaud Delobelle <arno...@gmail.com> wrote: > On 10 February 2012 06:21, Ian Kelly <ian.g.ke...@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- Hide quoted text - > > - Show quoted text - Thanks! Covers all bases, good.
[toc] | [prev] | [next] | [standalone]
| From | Alec Taylor <alec.taylor6@gmail.com> |
|---|---|
| Date | 2012-02-10 22:05 +1100 |
| Message-ID | <mailman.5651.1328871943.27778.python-list@python.org> |
| In reply to | #20130 |
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
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-02-09 22:29 -0500 |
| Message-ID | <mailman.5634.1328844605.27778.python-list@python.org> |
| In reply to | #20127 |
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
[toc] | [prev] | [next] | [standalone]
| From | Hrvoje Niksic <hniksic@xemacs.org> |
|---|---|
| Date | 2012-02-11 11:26 +0100 |
| Message-ID | <878vk93bun.fsf@xemacs.org> |
| In reply to | #20135 |
Terry Reedy <tjreedy@udel.edu> 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.
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-02-10 03:39 +0000 |
| Message-ID | <mailman.5637.1328845110.27778.python-list@python.org> |
| In reply to | #20127 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web