Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10167
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.009 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'exception.': 0.09; 'silently': 0.09; 'throw': 0.09; 'trailing': 0.09; 'wrote:': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:Convert': 0.16; 'zeroes': 0.16; "subject:' ": 0.16; 'pm,': 0.16; 'received:209.85.210.174': 0.19; 'received:mail- iy0-f174.google.com': 0.19; 'ignore': 0.21; 'header:In-Reply- To:1': 0.22; 'point,': 0.25; 'sat,': 0.28; 'message- id:@mail.gmail.com': 0.28; 'skip:i 30': 0.29; 'problem': 0.29; 'does': 0.32; 'to:addr:python-list': 0.34; 'there': 0.34; 'opposed': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'should': 0.39; "there's": 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.40; 'give': 0.60; 'cause': 0.69; 'safe.': 0.93 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=KbHH1kvkC1TTI8LYHz6o4dLIg+ouIgskYkf5HtSBKrk=; b=KzConcQI54HVtA/5XuhONpfk6UVaMqRuInew6WZLsv96CU2LYbdaH0TbK4hrVpyeDi Ba7d6R/uTUpgaVvfKk3DtuEUdL0vZvRLOYE/7a2xND+KXNjLQ5Xbo0g4qt9YDbEhC59g k2kCyKsSn+evfV07JrLkHcAMT66j8Tu9hN8VM= |
| MIME-Version | 1.0 |
| In-Reply-To | <4230ed8b-5173-4408-938f-3777dcd60588@t7g2000vbv.googlegroups.com> |
| References | <j08rn1$mep$1@dough.gmane.org> <mailman.1317.1311241673.1164.python-list@python.org> <4e281f97$0$16404$426a74cc@news.free.fr> <j09c5r$sna$1@reader1.panix.com> <mailman.1333.1311278441.1164.python-list@python.org> <fc7c6c59-d1ae-4e01-8f62-369f0db1d167@a31g2000vbt.googlegroups.com> <mailman.1387.1311364775.1164.python-list@python.org> <4230ed8b-5173-4408-938f-3777dcd60588@t7g2000vbv.googlegroups.com> |
| Date | Sat, 23 Jul 2011 17:42:31 +1000 |
| Subject | Re: Convert '165.0' to int |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| 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.1398.1311406954.1164.python-list@python.org> (permalink) |
| Lines | 18 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1311406954 news.xs4all.nl 23838 [2001:888:2000:d::a6]:48164 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:10167 |
Show key headers only | View raw
On Sat, Jul 23, 2011 at 4:53 PM, Frank Millman <frank@chagford.com> wrote:
> The problem with that is that it will silently ignore any non-zero
> digits after the point. Of course int(float(x)) does the same, which I
> had overlooked.
If you know that there will always be a trailing point, you can trim
off any trailing 0s, then trim off a trailing '.', and then cast to
int:
int(s.rstrip('0').rstrip('.'))
It'll remove only zeroes and then only periods, as opposed to
s.rstrip('.0') which would give a wrong result for (say) '40.000', so
this should be safe. If there's any non-zero digits after the decimal,
they and the decimal will be kept, which will cause the int() cast to
throw an exception.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Convert '165.0' to int Leo Jay <python.leojay@gmail.com> - 2011-07-21 17:47 +0800
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-21 04:05 -0700
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-21 04:23 -0700
Re: Convert '165.0' to int Web Dreamer <webdreamer@nospam.fr> - 2011-07-21 14:46 +0200
Re: Convert '165.0' to int Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-21 09:27 -0400
Re: Convert '165.0' to int Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-07-22 04:40 +0200
Re: Convert '165.0' to int Billy Mays <noway@nohow.com> - 2011-07-22 00:45 -0400
Re: Convert '165.0' to int Grant Edwards <invalid@invalid.invalid> - 2011-07-22 14:21 +0000
Re: Convert '165.0' to int Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-22 10:49 -0400
Re: Convert '165.0' to int Grant Edwards <invalid@invalid.invalid> - 2011-07-22 14:58 +0000
Re: Convert '165.0' to int Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-22 11:06 -0400
Re: Convert '165.0' to int Grant Edwards <invalid@invalid.invalid> - 2011-07-22 15:44 +0000
Re: Convert '165.0' to int Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-07-24 10:03 +0200
Re: Convert '165.0' to int Web Dreamer <webdreamer@nospam.fr> - 2011-07-25 10:23 +0200
Re: Convert '165.0' to int Web Dreamer <webdreamer@nospam.fr> - 2011-07-25 11:00 +0200
Re: Convert '165.0' to int Grant Edwards <invalid@invalid.invalid> - 2011-07-21 14:13 +0000
Re: Convert '165.0' to int Terry Reedy <tjreedy@udel.edu> - 2011-07-21 16:00 -0400
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-21 22:55 -0700
Re: Convert '165.0' to int Hrvoje Niksic <hniksic@xemacs.org> - 2011-07-22 14:42 +0200
Re: Convert '165.0' to int rantingrick <rantingrick@gmail.com> - 2011-07-22 12:32 -0700
Re: Convert '165.0' to int rantingrick <rantingrick@gmail.com> - 2011-07-22 12:34 -0700
Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-23 06:06 +1000
Re: Convert '165.0' to int Terry Reedy <tjreedy@udel.edu> - 2011-07-22 15:59 -0400
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-22 23:53 -0700
Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-23 17:42 +1000
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-23 02:03 -0700
Re: Convert '165.0' to int Billy Mays <noway@nohow.com> - 2011-07-23 11:12 -0400
Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-24 01:20 +1000
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-23 23:53 -0700
Re: Convert '165.0' to int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-24 17:34 +1000
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-24 00:58 -0700
Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-24 18:07 +1000
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-24 01:21 -0700
Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-24 18:28 +1000
Re: Convert '165.0' to int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-23 18:23 +1000
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-23 02:08 -0700
Re: Convert '165.0' to int rantingrick <rantingrick@gmail.com> - 2011-07-23 11:28 -0700
Re: Convert '165.0' to int Billy Mays <noway@nohow.com> - 2011-07-23 23:53 -0400
Re: Convert '165.0' to int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-24 17:21 +1000
Re: Convert '165.0' to int Ben Finney <ben+python@benfinney.id.au> - 2011-07-24 17:43 +1000
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-24 01:36 -0700
Re: Convert '165.0' to int Ben Finney <ben+python@benfinney.id.au> - 2011-07-24 18:53 +1000
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-24 02:01 -0700
Re: Convert '165.0' to int Ben Finney <ben+python@benfinney.id.au> - 2011-07-24 19:25 +1000
Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-24 19:42 +1000
Re: Convert '165.0' to int Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-07-25 12:04 +1200
Re: Convert '165.0' to int Frank Millman <frank@chagford.com> - 2011-07-24 22:50 -0700
RE: Convert '165.0' to int "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2011-08-01 20:42 -0400
RE: Convert '165.0' to int "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2011-08-01 20:32 -0400
Re: Convert '165.0' to int Web Dreamer <webdreamer@nospam.fr> - 2011-07-25 10:19 +0200
csiph-web