"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'] |