Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > round down to nearest number

Reply
Thread Tools

round down to nearest number

 
 
noydb
Guest
Posts: n/a
 
      02-10-2012
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?
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      02-10-2012
On Thu, Feb 9, 2012 at 5:30 PM, noydb <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
noydb
Guest
Posts: n/a
 
      02-10-2012
hmmm, okay.

So how would you round UP always? Say the number is 3219, so you want
3300 returned.
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      02-10-2012
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 %.

Cheers,
Chris
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      02-10-2012
On Thu, Feb 9, 2012 at 6:43 PM, 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 %.


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.
 
Reply With Quote
 
noydb
Guest
Posts: n/a
 
      02-10-2012
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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-10-2012
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

 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      02-10-2012
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.
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      02-10-2012
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
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      02-10-2012
On Thu, Feb 9, 2012 at 8:36 PM, MRAB <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Round a float to an nearest whole number Don Java 30 03-04-2007 08:23 AM
round to the nearest power of 4 naren2345@yahoo.com C Programming 4 01-29-2007 03:40 PM
round() to nearest .05 ? tertius Python 4 11-16-2004 10:58 AM
round number to nearest integer. Fred Java 3 06-06-2004 06:20 AM
round up to nearest number and significant figures Steve Java 5 05-17-2004 01:30 AM



Advertisments