Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Curious Omission In New-Style Formats Date: Wed, 13 Jul 2016 23:00:15 +1000 Lines: 26 Message-ID: References: <7fcc8c21-106f-41d4-a5ba-409f3b54a56d@googlegroups.com> <5783c91e$0$1622$c3e8da3$5496439d@news.astraweb.com> <57846636$0$1621$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de Q2A9WFBUZ86CyZmeK3ImYguazn0K3Azpm/y8NYYox/XA== Return-Path: 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; 'float': 0.05; 'cc:addr :python-list': 0.09; 'integer,': 0.09; 'integers': 0.09; 'output?': 0.09; 'toss': 0.09; 'wed,': 0.15; '"."': 0.16; '2016': 0.16; 'fixed-point': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; "skip:' 60": 0.16; 'wrote:': 0.16; 'integer': 0.18; '>>>': 0.20; 'math': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'accuracy': 0.23; 'insert': 0.23; 'split': 0.23; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'message-id:@mail.gmail.com': 0.27; 'converting': 0.27; 'operations,': 0.27; '13,': 0.29; 'decimal': 0.29; 'window': 0.30; "skip:' 20": 0.34; 'received:google.com': 0.35; 'pm,': 0.36; 'subject:: ': 0.37; 'doing': 0.38; 'format': 0.39; 'subject:-': 0.39; 'jul': 0.72; 'chrisa': 0.84; 'to:none': 0.91; 'dennis': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc; bh=wzvzSU0tQ5O5xIGUB1GkEkriRTKMewpshn7RPReeIeE=; b=lPWU3HCmfnWZYuVa/DdxOWtzFOTJ7SU8RSmz4UXYtKVpw8amHQs4evUr7lpiD9n41D J/Pd2Wo9PSiDShmFCtYMfG5JLN+wxIOr0EmbKAp5mGV77hr5GgzNJ6R5xvAbjkX1+UHy 0ZMILiv8VsxBovEFfitdE1ilRECW0o0+a6TS1/3XXdldhW2378irxBukfQ74uV9OlBfA DYECFz4o/Tu+JYIXa/89Ju20+vyBP2OmiO4nLO8jOzyO8S054by19IRyy3nwrVt1wVhF 0CS+e4WF/NaFoW0V21ImJEpFjECOXwnLCZi11olpBNJAHIven+xeFBeOAu15uz+P6L68 bXaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc; bh=wzvzSU0tQ5O5xIGUB1GkEkriRTKMewpshn7RPReeIeE=; b=QF+OrajZw164R+Nwm7SPAJYcJzX/0fg4wrxgMGG6SXlyWV5DWp1u6pOVbvcwNpVK5O Ou5CobzszKXIP50OWrLMukT4uFPXBBzWD3BvAgvvJ5XzWk95a6BUamtBfcSvaxU5ugqZ h+jDLU04XqqBV0k5t5/8KUpFYI0lGnhntNLnOCOTRuXn0Gmy/J3cazO2kHyR9YXfkUaU rtWI4deYrp62DWUtNra7emQefrnCPXHQTy7vF4eEVNPQJ94ufUi+b0lb3zw1rgF/O+zZ jHCqDEGJppc6A8QjqPiAnwpl7FadI3smD3geNTSq7jFYf/cKP6dGNwCQix/8LruSYnK5 cPSw== X-Gm-Message-State: ALyK8tK4aW9zO5LE7M+nHGTaK9diBW1kZZsZIWX4eJBdLokBnVF1fZ0f8ET+va8YMvTqsH3ePcEjgtDo5YH2YQ== X-Received: by 10.194.86.195 with SMTP id r3mr824171wjz.30.1468414817678; Wed, 13 Jul 2016 06:00:17 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <7fcc8c21-106f-41d4-a5ba-409f3b54a56d@googlegroups.com> <5783c91e$0$1622$c3e8da3$5496439d@news.astraweb.com> <57846636$0$1621$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:111392 On Wed, Jul 13, 2016 at 9:46 PM, Dennis Lee Bieber wrote: > On Wed, 13 Jul 2016 00:21:17 -0600, Ian Kelly > declaimed the following: > >>What if I've been doing my math with fixed-point integers (because I >>don't know about or just don't like decimals), and now I want to >>format them for output? Is this just wrong? >> >> '{:.2f}'.format(int_value / 100) >> > > Ugh... After using integers to keep accuracy in the LSB, you know toss > it out the window by converting to a float which may have an inexact > representation. > > Presuming you kept track of the decimal place during all those integer > operations, format as an integer, then split it at the decimal and insert a > "." at the spot. Or just use divmod: >>> "%d.%02d" % divmod(1<<200, 100) '16069380442589902755419620923411626025222029937827928353013.76' ChrisA