This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Keith.Brafford
Recipients Keith.Brafford, eric.smith, ezio.melotti, hardkrash, mark.dickinson, r.david.murray, terry.reedy
Date 2010-04-27.19:50:15
SpamBayes Score 0.0
Marked as misclassified No
Message-id <1272397826.52.0.418826825409.issue8060@psf.upfronthosting.co.za>
In-reply-to
Content
Ok, let's zero in on how this should work.  I'll start the concrete proposal discussion in terms of how it would have worked with the old-style specifiers (since I am more familiar with that method) and we can bring it up to Py3K standards as a group.

I was thinking something along these lines:
%[<space> | 0 ][.precision]m

where the optional space or zero means that you want the whole number part of the output to be padded to three digits, either with spaces or prepended zeroes.  No space or zero means no prepending of anything.

The .precision would be the number of digits after the decimal point you want to see.  If you don't specify this, then the default would be something that people agreed made the most sense, say 4 for now.

f = math.pi * 1e-5
print f
3.1415926535897935e-005
print "%m" % f
31.4159e-06
print "% m" % f
  31.4159e-06
print "%0m" % f
031.4159e-06
print "% m.6" % f
  31.415927e-06

Mark brought up this point:

e.g. format(12345.678, '.5m'):  Should the '5' indicate 5 digits
after the point (giving '12.34568e+3' in this case), or 5 significant
digits in total (giving '12.345e+3').  

I tend to think that it's more important that the precision number tell you the number of digits after the decimal point.  This is because the underlying float still has all of the precision, and the format specifier is used simply to make the printout look correct.  Being able to specify that you want three slots before the point, then a constant number after the point lets you get perfectly aligned columns in a tabular printout without a lot of fuss.

I wrote a class that I've been using to test these formats out with (attached).  I use it like this:

>>> from efloat impot EFloat as E

it has a class "precision" value that you can set:

>>> E.precision = 5
>>> E(math.pi)
3.14159e+0
>>> E(math.pi / 1e-9)
3.14159e+9
>>> E(math.pi / 1e-2)
314.15927e+0
>>> E(math.pi / 1e-5)
314.15927e+3

It has a slight bug, though.  It doesn't give me two digits of exponent, which would be required in the engineering format specifier, IMHO, so that programmers can easily get constant tabular column widths.
History
Date User Action Args
2010-04-27 19:50:27Keith.Braffordsetrecipients: + Keith.Brafford, terry.reedy, mark.dickinson, eric.smith, ezio.melotti, r.david.murray, hardkrash
2010-04-27 19:50:26Keith.Braffordsetmessageid: <1272397826.52.0.418826825409.issue8060@psf.upfronthosting.co.za>
2010-04-27 19:50:19Keith.Braffordlinkissue8060 messages
2010-04-27 19:50:17Keith.Braffordcreate