Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85651
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.030 |
| X-Spam-Evidence | '*H*': 0.94; '*S*': 0.00; 'subject:not': 0.03; 'trailing': 0.09; 'try:': 0.09; 'def': 0.12; '...will': 0.16; 'exponential': 0.16; 'notation': 0.16; 'subject:format': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'feb': 0.22; '>>>': 0.22; 'import': 0.22; 'fraction': 0.24; 'instead.': 0.24; 'rid': 0.24; 'specify': 0.24; 'string,': 0.24; 'pass': 0.26; 'skip:" 20': 0.27; 'header :In-Reply-To:1': 0.27; 'rest': 0.29; 'am,': 0.29; 'raise': 0.29; "doesn't": 0.30; 'dec': 0.30; 'specified': 0.30; 'message- id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '13,': 0.31; 'decimal': 0.31; 'large.': 0.31; 'skip:r 60': 0.31; 'writes:': 0.31; 'up.': 0.33; 'fri,': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'received:209.85': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'consistent': 0.36; 'module.': 0.36; 'method': 0.36; 'thanks': 0.36; 'too': 0.37; 'received:209': 0.37; 'to:addr:python-list': 0.38; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'ian': 0.60; "you're": 0.61; 'first': 0.61; 'telling': 0.64; 'side': 0.67; 'skip:r 30': 0.69; '2015': 0.84; 'clearing': 0.91; 'obtained': 0.96 |
| 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:to :content-type:content-transfer-encoding; bh=jVXiPgJXYFFRyZVx1oYWeuL9M0Ud2R/9guKjzc4C5Ts=; b=mHfkqK1Exf6Mde7WdXLCyMObFYMuLSTcFz1vtcoGbLssvTnJDLFAs598+iZAvX1b4w HLM2Xn4ng3vRtqsSCL3MDYh47WYSu/RfcBUMHi3IGokgHZgSTW/LJ1BuuZfo3p2Tit9Y QfjE+PgGGs6WVpFgzrjtpATE0vT4ECo+Nwkrlpv0eUEMfbQPGyYKh6zXU3+9i2nrjCHr w3iKr9dgBiUDTes7pQy1NMNevfUxTUIENq0K8I78Tq+cz17NloJg7rnO0a96U5TRohMJ +ro4pPWx6tANNfHBAJIlI9XERsrTS6ouU0/jJIezcKkKigPgllDsvwKJuE22IB8X9QFY ZCrA== |
| X-Received | by 10.66.65.234 with SMTP id a10mr17264041pat.120.1423846370797; Fri, 13 Feb 2015 08:52:50 -0800 (PST) |
| MIME-Version | 1.0 |
| In-Reply-To | <CAK291foC1w=ogg6qf=7cFkdn=7AB+3U4d2Y56MMvKDr_sVkBkg@mail.gmail.com> |
| References | <CAK291fpAycSecNuV6hDnVXK+rhB0CWJTAuC3BKNUz8B4aDg2kQ@mail.gmail.com> <CAK291fpKxW_V=yCvNHj9RRoEhC=i_DYJCa9-cbjxDwRwH4-KfQ@mail.gmail.com> <CAK291foC1w=ogg6qf=7cFkdn=7AB+3U4d2Y56MMvKDr_sVkBkg@mail.gmail.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Fri, 13 Feb 2015 09:52:10 -0700 |
| Subject | Re: Floating point "g" format not stripping trailing zeros |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.18730.1423846374.18130.python-list@python.org> (permalink) |
| Lines | 57 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1423846374 news.xs4all.nl 2962 [2001:888:2000:d::a6]:46541 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:85651 |
Show key headers only | View raw
On Fri, Feb 13, 2015 at 2:26 AM, Hrvoje Nikšić <hniksic@gmail.com> wrote:
> Ian Kelly writes:
>> When you specify the a precision of 15 in your format string, you're
>> telling it to take the first 15 of those. It doesn't care that the
>> last couple of those are zeros, because as far as it's concerned,
>> those digits are significant.
>
> OK, it's a bit surprising, but also consistent with the rest of the
> decimal module. Thanks for clearing it up.
>
> My concrete use case is printing an arbitrary fraction as a
> user-readable decimal, rounded to the specified number of digits, and
> using the exponential notation where appropriate:
>
> import decimal
> _dec_fmt_context = decimal.Context(prec=15, rounding=decimal.ROUND_HALF_UP)
> def _format(frac):
> with decimal.localcontext(_dec_fmt_context):
> dec = decimal.Decimal(frac.numerator) /
> decimal.Decimal(frac.denominator)
> return '{:g}'.format(dec)
>
> The decimal obtained by dividing the numerator with the denominator
> includes trailing zeros. Calling normalize() to get rid of them will
> have the unfortunate side effect of turning 9806650 into 9.80665e+6,
> and the method recommended in the documentation:
>
> def remove_exponent(d):
> return d.quantize(decimal.Decimal(1)) if d == d.to_integral() else
> d.normalize()
>
> ...will raise "decimal.InvalidOperation: quantize result has too many
> digits for current context" when the number is too large.
In that case I think you just want to normalize instead. So:
>>> def remove_exponent(d):
... if d == d.to_integral():
... try:
... return d.quantize(1)
... except decimal.InvalidOperation:
... pass
... return d.normalize()
...
>>> remove_exponent(D('123000000'))
Decimal('123000000')
>>> remove_exponent(D('123.4567'))
Decimal('123.4567')
>>> remove_exponent(D('123.4567890'))
Decimal('123.456789')
>>> remove_exponent(D('123e6'))
Decimal('123000000')
>>> remove_exponent(D('1234567890123456789012345678901234567890'))
Decimal('1.234567890123456789012345679E+39')
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Floating point "g" format not stripping trailing zeros Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-13 09:52 -0700
csiph-web