Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
round down to nearest number
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
noydb  
View profile  
 More options Feb 9 2012, 7:30 pm
Newsgroups: comp.lang.python
From: noydb <jenn.du...@gmail.com>
Date: Thu, 9 Feb 2012 16:30:53 -0800 (PST)
Local: Thurs, Feb 9 2012 7:30 pm
Subject: round down to nearest number
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?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Feb 9 2012, 7:47 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Thu, 9 Feb 2012 17:47:09 -0700
Local: Thurs, Feb 9 2012 7:47 pm
Subject: Re: round down to nearest number

On Thu, Feb 9, 2012 at 5:30 PM, noydb <jenn.du...@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
noydb  
View profile  
 More options Feb 9 2012, 8:23 pm
Newsgroups: comp.lang.python
From: noydb <jenn.du...@gmail.com>
Date: Thu, 9 Feb 2012 17:23:46 -0800 (PST)
Local: Thurs, Feb 9 2012 8:23 pm
Subject: Re: round down to nearest number
hmmm, okay.

So how would you round UP always?  Say the number is 3219, so you want
3300 returned.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Feb 9 2012, 8:43 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Thu, 9 Feb 2012 17:43:58 -0800
Local: Thurs, Feb 9 2012 8:43 pm
Subject: Re: round down to nearest number

On Thu, Feb 9, 2012 at 5:23 PM, noydb <jenn.du...@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-o...

Thus: (3219 + 99) // 100

Slight tangent: Beware negative numbers when using // or %.

Cheers,
Chris


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Feb 9 2012, 9:00 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Thu, 9 Feb 2012 19:00:37 -0700
Local: Thurs, Feb 9 2012 9:00 pm
Subject: Re: round down to nearest number

On Thu, Feb 9, 2012 at 6:43 PM, Chris Rebert <c...@rebertia.com> wrote:
> On Thu, Feb 9, 2012 at 5:23 PM, noydb <jenn.du...@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-o...

> 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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
noydb  
View profile  
 More options Feb 9 2012, 9:25 pm
Newsgroups: comp.lang.python
From: noydb <jenn.du...@gmail.com>
Date: Thu, 9 Feb 2012 18:25:56 -0800 (PST)
Local: Thurs, Feb 9 2012 9:25 pm
Subject: Re: round down to nearest number
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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Feb 9 2012, 10:29 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Thu, 09 Feb 2012 22:29:05 -0500
Local: Thurs, Feb 9 2012 10:29 pm
Subject: Re: round down to nearest number
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Feb 9 2012, 10:36 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Fri, 10 Feb 2012 03:36:11 +0000
Local: Thurs, Feb 9 2012 10:36 pm
Subject: Re: round down to nearest number
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Feb 9 2012, 10:39 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Fri, 10 Feb 2012 03:39:08 +0000
Local: Thurs, Feb 9 2012 10:39 pm
Subject: Re: round down to nearest number
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Feb 10 2012, 1:21 am
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Thu, 9 Feb 2012 23:21:31 -0700
Local: Fri, Feb 10 2012 1:21 am
Subject: Re: round down to nearest number

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arnaud Delobelle  
View profile  
 More options Feb 10 2012, 4:58 am
Newsgroups: comp.lang.python
From: Arnaud Delobelle <arno...@gmail.com>
Date: Fri, 10 Feb 2012 09:58:35 +0000
Local: Fri, Feb 10 2012 4:58 am
Subject: Re: round down to nearest number
On 10 February 2012 06:21, Ian Kelly <ian.g.ke...@gmail.com> wrote:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alec Taylor  
View profile  
 More options Feb 10 2012, 6:05 am
Newsgroups: comp.lang.python
From: Alec Taylor <alec.tayl...@gmail.com>
Date: Fri, 10 Feb 2012 22:05:39 +1100
Local: Fri, Feb 10 2012 6:05 am
Subject: Re: round down to nearest number
o.O

Very nice


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
noydb  
View profile  
 More options Feb 10 2012, 12:23 pm
Newsgroups: comp.lang.python
From: noydb <jenn.du...@gmail.com>
Date: Fri, 10 Feb 2012 09:23:21 -0800 (PST)
Local: Fri, Feb 10 2012 12:23 pm
Subject: Re: round down to nearest number
On Feb 10, 4:58 am, Arnaud Delobelle <arno...@gmail.com> wrote:

Thanks!  Covers all bases, good.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Olive  
View profile  
 More options Feb 10 2012, 3:56 pm
Newsgroups: comp.lang.python
From: Olive <di...@bigfoot.com>
Date: Fri, 10 Feb 2012 21:56:44 +0100
Local: Fri, Feb 10 2012 3:56 pm
Subject: Re: round down to nearest number
On Thu, 9 Feb 2012 17:43:58 -0800

Chris Rebert <c...@rebertia.com> wrote:
> On Thu, Feb 9, 2012 at 5:23 PM, noydb <jenn.du...@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-o...

> 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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hrvoje Niksic  
View profile  
 More options Feb 11 2012, 5:26 am
Newsgroups: comp.lang.python
From: Hrvoje Niksic <hnik...@xemacs.org>
Date: Sat, 11 Feb 2012 11:26:08 +0100
Local: Sat, Feb 11 2012 5:26 am
Subject: Re: round down to nearest number

Terry Reedy <tjre...@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »