Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'digest': 0.04; 'string': 0.09; 'assuming': 0.09; 'converted': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'useless': 0.09; 'finney': 0.16; 'formatted': 0.16; 'intrinsic': 0.16; 'partly': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:Converting': 0.16; 'result.': 0.19; '>>>': 0.22; 'header :User-Agent:1': 0.23; 'specify': 0.24; 'text.': 0.24; 'skip:" 20': 0.27; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'especially': 0.30; 'religious': 0.31; 'writes:': 0.31; "we're": 0.32; 'text': 0.33; 'url:python': 0.33; 'cases': 0.33; 'standards': 0.33; 'convert': 0.35; 'but': 0.35; 'there': 0.35; 'choosing': 0.36; 'vice': 0.36; 'url:org': 0.36; 'should': 0.36; 'ben': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'most': 0.60; 'new': 0.61; 'url:3': 0.61; 'simple': 0.61; 'different': 0.65; 'skip:\xe2 10': 0.65; 'talking': 0.65; 'received:125': 0.84; 'canonical': 0.91; '\xe2\x80\x9cthe': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Converting 5.223701009526849e-05 to 5e-05 Date: Sun, 03 May 2015 18:40:38 +1000 References: <87vbgakrlr.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:M1nE6Ppkyid5SzGSGSDRp6riudo= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430642455 news.xs4all.nl 2834 [2001:888:2000:d::a6]:54786 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89836 Cecil Westerhof writes: > When I have a value like 5.223701009526849e-05 in most cases I am not > interested in all the digest after the dot. What type of value is it? A ‘float’ value has many different textual representations, most of them inaccurate. So talking about the digits of a ‘float’ value is only partly meaningful; digits are a property of some chosen representation, not intrinsic to the number. A ‘str’ value can be converted in various ways, but is useless as a number until you create a new number from the result. Choosing a solution will rely on understanding that the textual representation of a number is not itself a number; and vice versa, a number value does not have a canonical text representation. > Is there a simple way to convert it to a string like '5e-05'? Assuming we're talking about a ‘float’ value:: >>> foo = 5.223701009526849e-05 >>> "{foo:5.1}".format(foo=foo) '5e-05' See the ‘str.format’ documentation, especially the detailed documentation for the “format specification mini-language” for how to specify exactly how you want values to be formatted as text. -- \ “The double standard that exempts religious activities from | `\ almost all standards of accountability should be dismantled | _o__) once and for all.” —Daniel Dennett, 2010-01-12 | Ben Finney