int('0.0') throws an exception

Bjorn Pettersen BPettersen at NAREX.com
Wed Jan 29 19:28:02 EST 2003


> From: Ragu Bharadwaj [mailto:Ragu.Bharadwaj at amphoracorp.com] 
> 
> Why is it that
> int('0') and int(0.0) are OK but int('0.0') throws an Exception?

It behaves exactly as documented (Library Ref. 2.1): "If the argument is
a string, it must contain a possibly signed decimal number representable
as a Python integer...". Basically, it is only doing a one-step
conversion (whereas "3.14" -> 3.14 -> 3 would be a two-step conversion).
You can easily get the functionality you expect by:

  x = int(float('3.14'))

-- bjorn





More information about the Python-list mailing list