Convert '165.0' to int

Frank Millman frank at chagford.com
Thu Jul 21 07:09:35 EDT 2011


On Jul 21, 11:53 am, Thomas Jollans <t... at jollybox.de> wrote:
> On 21/07/11 11:31, Frank Millman wrote:
>
> > Hi all
>
> > I want to convert '165.0' to an integer.
>
> Well, it's not an integer. What does your data look like? How do you
> wish to convert it to int? Do they all represent decimal numbers? If so,
> how do you want to round them? What if you get '165.xyz' as input?
> Should that raise an exception? Should it evaluate to 165? Should it use
> base 36?
>
> > If I convert to a float first, it does work -
>
> >>>> int(float(x))
> > 165
>
> > Is there a short cut, or must I do this every time (I have lots of
> > them!) ? I know I can write a function to do this, but is there anything
> > built-in?
>
> What's wrong with this? It's relatively concise, and it shows exactly
> what you're trying to do.

I am processing an xml file from a third party, and it is full of co-
ordinates in the form 'x="165.0" y="229.0"'.

I don't mind using int(float(x)), I just wondered if there was a
shorter alternative.

If there is an alternative, I will be happy for it to raise an
exception if the fractional part is not 0.

Thanks

Frank



More information about the Python-list mailing list