Velocity Reviews > round down to nearest number

# round down to nearest number

Arnaud Delobelle
Guest
Posts: n/a

 02-10-2012
On 10 February 2012 06:21, Ian Kelly <(E-Mail Removed)> 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

Alec Taylor
Guest
Posts: n/a

 02-10-2012
o.O

Very nice

On Fri, Feb 10, 2012 at 8:58 PM, Arnaud Delobelle <(E-Mail Removed)> wrote:
> On 10 February 2012 06:21, Ian Kelly <(E-Mail Removed)> 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

noydb
Guest
Posts: n/a

 02-10-2012
On Feb 10, 4:58*am, Arnaud Delobelle <(E-Mail Removed)> wrote:
> On 10 February 2012 06:21, Ian Kelly <(E-Mail Removed)> 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.

Olive
Guest
Posts: n/a

 02-10-2012
On Thu, 9 Feb 2012 17:43:58 -0800
Chris Rebert <(E-Mail Removed)> wrote:

> On Thu, Feb 9, 2012 at 5:23 PM, noydb <(E-Mail Removed)> wrote:
> > hmmm, okay.
> >
> > So how would you round UP always? *Say the number is 3219, so you
> > want 3300 returned.

>
> http://stackoverflow.com/questions/1...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

Hrvoje Niksic
Guest
Posts: n/a

 02-11-2012
Terry Reedy <(E-Mail Removed)> 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.

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Don Java 30 03-04-2007 08:23 AM naren2345@yahoo.com C Programming 4 01-29-2007 03:40 PM tertius Python 4 11-16-2004 10:58 AM Fred Java 3 06-06-2004 06:20 AM Steve Java 5 05-17-2004 01:30 AM

Advertisments