[issue8060] PEP 3101 string formatting missing engineering presentation type for floating point
report at bugs.python.org
Tue Apr 27 21:50:26 CEST 2010
Keith Brafford <keith.brafford at gmail.com> added the comment:
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 "%m" % f
print "% m" % f
print "%0m" % f
print "% m.6" % f
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 / 1e-9)
>>> E(math.pi / 1e-2)
>>> E(math.pi / 1e-5)
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.
Added file: http://bugs.python.org/file17111/efloat.py
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list