[Python-Dev] Rounding float to int directly (Re: struct module and coercing floats to integers)

Ka-Ping Yee python-dev at zesty.ca
Thu Aug 3 21:02:45 CEST 2006


On Thu, 3 Aug 2006, Greg Ewing wrote:
> The time isn't the main issue. The main issue
> is that almost all the use cases for round()
> involve doing an int() on it afterwards.

That's my experience as well.  In my opinion, the purpose of round()
is most commonly described as "to make an integer".  So it should
yield an integer.

As other comments in this discussion imply, whether you want round()
to return an integer really depends on your intended audience and
their pattern of usage.

It seems to me that for round() to produce an integer is about as
intuitive as for 1/2 to produce 0.5.  That is, if you bring basic
mathematical intuition to the table instead of a background in how
C behaves, then "1/2" suggests one-half and "rounding" suggests
"convert to an integer".  If you have a background in programming
languages and you understand the value of type consistency, then
you would see why dividing integers should yield integers and
rounding floats should yield floats.

Since Python was indeed changed to make 1/2 evaluate to 0.5, that
suggests that it is Pythonic to target the first audience rather
than the second.  And that implies that it would also be Pythonic
to make round() return an int.


-- ?!ng


More information about the Python-Dev mailing list