[issue20502] Context.create_decimal_from_float() inconsistent precision for zeros after decimal mark

Mauricio de Alencar report at bugs.python.org
Tue Feb 4 15:19:44 CET 2014


Mauricio de Alencar added the comment:

"Digits after the decimal mark" is not the same as "significant digits".
See https://en.wikipedia.org/wiki/Significant_figures

If I have a list of numbers [256.2, 1.3, 0.5] that have 3 significant digits each, I would like to have them displayed as:
['256', '1.30', '0.500']


['{:.2e}'.format(_) for _ in [256.2, 1.3, 0.5]]

would print:

['2.56e+02', '1.30e+00', '5.00e-01']

Which gets the digits right, but is not as desired.

But if I use


from decimal import Context

def dec(num, prec):
    return Context(prec=prec).create_decimal('{{:.{:d}e}}'.format(prec - 1).format(num))

[str(dec(_, 3)) for _ in [256.2, 1.3, 0.5]]


The the output is:
['256', '1.30', '0.500']

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20502>
_______________________________________


More information about the Python-bugs-list mailing list