int('2.1') does not work while int(float('2.1')) does

Duncan Booth me at privacy.net
Tue Apr 6 07:52:32 EDT 2004


Joe Mason <joe at notcharles.ca> wrote in 
news:slrnc74t4i.ccv.joe at gate.notcharles.ca:

> In article <40722E98.253B5FF2 at alcyone.com>, Erik Max Francis wrote:
>> Joe Mason wrote:
>> 
>>> Why can it make this guess for "int(2.1)", then?  It's got a rule for
>>> converting floats to ints - why not use it here?
>> 
>> Because int(aFloat) means round toward zero.  int(aString) means make an
>> int out of this string.
> 
> So why can't int(string) mean round toward zero?
> 

If you want to round towards zero then you have an easy way to do it:

   int(float(string))

If int(string) was changed to have this behaviour as well, then those of 
who don't want any rounding wouldn't have any way to get the current 
behaviour. Users may be surprised when they enter 2.1 and find the program 
accepted it but didn't use the value they entered; I don't like suprising 
users.

Or in more concise terms:

    Explicit is better than implicit.




More information about the Python-list mailing list