Cheat sheet for the new string formatting?

Yann Kaiser kaiser.yann at gmail.com
Mon Jun 8 16:47:28 EDT 2015


It just means significant digits in the general format, which alternates
between 10-exponent notation and plain decimal notation.

https://docs.python.org/3.4/library/string.html#format-specification-mini-language

    >>> '{:.3}'.format(0.0000356785)
    '3.57e-05'
    >>> '{:.3}'.format(0.00356785)
    '0.00357'

On Mon, 8 Jun 2015 at 22:33 Skip Montanaro <skip.montanaro at gmail.com> wrote:

> This is counterintuitive:
>
> >>> "{:.3}".format(-0.00666762259822)
> '-0.00667'
> >>> "{:.3f}".format(-0.00666762259822)
> '-0.007'
> >>> "%.3f" % -0.00666762259822
> '-0.007'
> >>> "{:.3s}".format(-0.00666762259822)
> ValueError Unknown format code 's' for object of type 'float'
>
> Why does the first form display five digits after the decimal point? Why
> don't floats support "{:.Ns}"? (I know I can use "{!s}".)
>
> This is using a freshly hg pulled and updated 2.7  branch.
>
> Thx,
>
> S
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150608/b7c1f77c/attachment.html>


More information about the Python-list mailing list