Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #40605
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'float': 0.05; "'')": 0.07; 'skip:% 20': 0.07; "'''": 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'spec': 0.09; 'subject:number': 0.09; 'terry': 0.09; 'width.': 0.09; 'zeros': 0.09; 'def': 0.10; 'exponent': 0.16; 'exponent.': 0.16; 'interprets': 0.16; 'need:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'spec)': 0.16; 'spec):': 0.16; 'spec,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'pieces': 0.17; 'jan': 0.18; '>>>': 0.18; 'suggested': 0.20; 'import': 0.21; 'latter': 0.22; 'example': 0.23; '15,': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'possibly': 0.27; 'opposed': 0.27; 'header:X-Complaints-To:1': 0.28; '1.4': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "skip:' 10": 0.30; 'function': 0.30; 'code': 0.31; 'could': 0.32; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'formats': 0.35; 'pm,': 0.35; 'add': 0.36; 'received:org': 0.36; '12,': 0.36; 'method': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'system.': 0.39; 'to:addr:python.org': 0.39; 'notice': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'real': 0.61; 'kind': 0.61; 'customized': 0.64; 'of:': 0.65; 'yourself,': 0.75; 'received:fios.verizon.net': 0.84; 'tie': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: Controlling number of zeros of exponent in scientific notation |
| Date | Wed, 06 Mar 2013 00:45:10 -0500 |
| References | <c2184b42-41be-4930-9501-361296df7679@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-173-75-251-66.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 |
| In-Reply-To | <c2184b42-41be-4930-9501-361296df7679@googlegroups.com> |
| 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 | <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.2924.1362548730.2939.python-list@python.org> (permalink) |
| Lines | 60 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1362548730 news.xs4all.nl 6949 [2001:888:2000:d::a6]:45891 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:40605 |
Show key headers only | View raw
On 3/5/2013 3:09 PM, faraz@squashclub.org wrote:
> Instead of:
> 1.8e-04
> I need:
> 1.8e-004
>
> So two zeros before the 4, instead of the default 1.
The standard e and g float formats do not give you that kind of control
over the exponent. You have to write code that forms the string you
want. You can put that in a simple function or make a class with a
__format__ method to tie into the new format() and str.format system.
>>> class myfloat(float):
def __format__(self, spec): return '3.14'
>>> x = myfloat(1.4)
>>> x
1.4
>>> format(x, '')
'3.14'
>>> '{}'.format(x)
'3.14'
You could write __format__ to use standard format specs and then adjust:
def __format__(self, spec):
s = float.__format__(self, spec)
<adjust s>
return s
or generate the pieces of the string yourself, possibly using a custom
spec, as opposed to the standard spec. Notice this example from the manual:
'''
Using type-specific formatting:
>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'
'''
This works because datetime.datetime and a .__format__ that interprets a
highly customized format spec. You could customize myfloat's spec to add
a value for the exponent width. Your example above might be '8.1.3e'
instead of the standard '7.1e'
Standard, real:
>>> format(1.8e-4, '7.1e')
'1.8e-04'
Custom, hypothetical:
>>> format(myfloat(1.8e-4), '8.1.3e')
'1.8e-004'
Dave already suggested how you could write part of .__format__ to make
the latter be real also.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Controlling number of zeros of exponent in scientific notation faraz@squashclub.org - 2013-03-05 12:09 -0800
Re: Controlling number of zeros of exponent in scientific notation Dave Angel <davea@davea.name> - 2013-03-05 15:56 -0500
Re: Controlling number of zeros of exponent in scientific notation Terry Reedy <tjreedy@udel.edu> - 2013-03-06 00:45 -0500
Re: Controlling number of zeros of exponent in scientific notation Roy Smith <roy@panix.com> - 2013-03-06 09:03 -0500
Re: Controlling number of zeros of exponent in scientific notation jmfauth <wxjmfauth@gmail.com> - 2013-03-06 07:16 -0800
Re: Controlling number of zeros of exponent in scientific notation "Russ P." <Russ.Paielli@gmail.com> - 2013-03-06 11:11 -0800
csiph-web