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 mdealencar
Recipients mark.dickinson, mdealencar, skrah
Date 2014-02-04.14:19:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1391523584.21.0.835511786649.issue20502@psf.upfronthosting.co.za>
In-reply-to
Content
"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']
History
Date User Action Args
2014-02-04 14:19:44mdealencarsetrecipients: + mdealencar, mark.dickinson, skrah
2014-02-04 14:19:44mdealencarsetmessageid: <1391523584.21.0.835511786649.issue20502@psf.upfronthosting.co.za>
2014-02-04 14:19:44mdealencarlinkissue20502 messages
2014-02-04 14:19:44mdealencarcreate