Help me please : Rounding-down numbers

Bengt Richter bokr at oz.net
Thu Mar 27 07:18:44 CET 2003


On Wed, 26 Mar 2003 18:19:14 -0800, Erik Max Francis <max at alcyone.com> wrote:

>Skip Montanaro wrote:
>
>> There is also the floor() function in the math module:
>> 
>>     >>> math.floor(4.71)
>>     4.0
>>     >>> int(4.71)
>>     4
>
>Note that math.floor and int don't have quite the same behavior
>(glossing over the obvious difference that the former returns a float)
>for the case of negative numbers.
>
E.g.,

 >>> map(int,[-1.9, -1.5, -1.1,  -0.5, 0.0, 0.5, 1.1, 1.5, 1.9])
 [-1, -1, -1, 0, 0, 0, 1, 1, 1]
 >>> from math import floor
 >>> map(floor,[-1.9, -1.5, -1.1,  -0.5, 0.0, 0.5, 1.1, 1.5, 1.9])
 [-2.0, -2.0, -2.0, -1.0, 0.0, 0.0, 1.0, 1.0, 1.0]


Regards,
Bengt Richter




More information about the Python-list mailing list