Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10012

Convert '165.0' to int

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'valueerror:': 0.09; 'float': 0.13; '10:': 0.16; 'int(x)': 0.16; 'integer.': 0.16; 'subject:Convert': 0.16; "subject:' ": 0.16; '>>>': 0.16; 'convert': 0.19; '(most': 0.21; 'literal': 0.23; 'invalid': 0.25; 'traceback': 0.25; 'function': 0.26; 'thanks': 0.31; 'does': 0.32; 'to:addr:python- list': 0.34; 'header:X-Complaints-To:1': 0.34; 'there': 0.34; 'last):': 0.35; 'file': 0.36; 'anything': 0.37; 'but': 0.37; 'received:org': 0.38; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'received:41': 0.71
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From "Frank Millman" <frank@chagford.com>
Subject Convert '165.0' to int
Date Thu, 21 Jul 2011 11:31:56 +0200
Mime-Version 1.0
Content-Type text/plain; format=flowed; charset="iso-8859-1"; reply-type=original
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 41-135-212-38.dsl.mweb.co.za
X-MSMail-Priority Normal
X-Newsreader Microsoft Outlook Express 6.00.3790.4657
X-MimeOLE Produced By Microsoft MimeOLE V6.00.3790.4862
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1315.1311240764.1164.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311240764 news.xs4all.nl 23858 [2001:888:2000:d::a6]:43980
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10012

Show key headers only | View raw


Hi all

I want to convert '165.0' to an integer.

The obvious method does not work -

>>> x = '165.0'
>>> int(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '165.0'

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?

Thanks

Frank Millman

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Convert '165.0' to int "Frank Millman" <frank@chagford.com> - 2011-07-21 11:31 +0200
  Re: Convert '165.0' to int SigmundV <sigmundv@gmail.com> - 2011-07-24 11:27 -0700
    Re: Convert '165.0' to int Billy Mays <noway@nohow.com> - 2011-07-24 20:07 -0400
      Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-25 15:46 +1000
      Re: Convert '165.0' to int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-25 19:48 +1000
        Re: Convert '165.0' to int SigmundV <sigmundv@gmail.com> - 2011-07-25 09:39 -0700
        Re: Convert '165.0' to int Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-25 13:11 -0400

csiph-web