noydb | 10 Feb 03:25
Picon

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
MRAB | 10 Feb 04:36

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.
Ian Kelly | 10 Feb 07:21
Picon

Re: round down to nearest number

On Thu, Feb 9, 2012 at 8:36 PM, MRAB <python <at> 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
(Continue reading)

Arnaud Delobelle | 10 Feb 10:58
Picon

Re: round down to nearest number

On 10 February 2012 06:21, Ian Kelly <ian.g.kelly <at> 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)

(Continue reading)

Alec Taylor | 10 Feb 12:05
Picon

Re: round down to nearest number

o.O

Very nice

On Fri, Feb 10, 2012 at 8:58 PM, Arnaud Delobelle <arnodel <at> gmail.com> wrote:
> On 10 February 2012 06:21, Ian Kelly <ian.g.kelly <at> 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
>
(Continue reading)


Gmane